-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconcatenate-circular-lists.py
87 lines (61 loc) · 1.99 KB
/
concatenate-circular-lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright (C) Deepali Srivastava - All Rights Reserved
# This code is part of DSA course available on CourseGalaxy.com
class Node(object):
def __init__(self,value):
self.info = value
self.link = None
class CircularLinkedList(object):
def __init__(self):
self.last = None
def display_list(self):
if self.last == None:
print("List is empty\n")
return
p = self.last.link
while True:
print(p.info , " ",end='')
p = p.link
if p == self.last.link:
break
print()
def insert_in_empty_list(self,data):
temp = Node(data)
self.last = temp
self.last.link = self.last
def insert_at_end(self,data):
temp = Node(data)
temp.link = self.last.link
self.last.link = temp
self.last = temp
def create_list(self):
n = int(input("Enter the number of nodes : "))
if n == 0:
return
data = int(input("Enter the element to be inserted : "))
self.insert_in_empty_list(data)
# for(i = 2 i <= n i++)
for i in range(n-1):
data = int(input("Enter the element to be inserted : "))
self.insert_at_end(data)
def concatenate(self,list2):
if self.last is None:
self.last = list.last;
return
if list2.last is None:
return
p = self.last.link;
self.last.link = list2.last.link;
list2.last.link = p;
self.last = list2.last;
###############################################################
list1 = CircularLinkedList()
list2 = CircularLinkedList()
print("Enter first list :- ")
list1.create_list()
print("Enter second list :- ")
list2.create_list()
print("First "); list1.display_list()
print("Second "); list2.display_list()
list1.concatenate(list2)
print("First ")
list1.display_list()