Skip to content

Commit 623a475

Browse files
authored
Concatenation of 2 lists
code for concatenation: ~single linked list ~circular linked list
1 parent 9b7c64e commit 623a475

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

Concatenation of 2 list

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# CONCATENATION OF LINKED LIST
2+
3+
# A.SINGLE LINKED LIST
4+
5+
class Node:
6+
def __init__(self,value): #initialising the node |data_|link_|
7+
self.data=value
8+
self.link=None
9+
10+
class singleLinkedList:
11+
def __init__(self): #initialising the root element
12+
self.root=None
13+
14+
def displayList(self): #for displaying the list elements
15+
if self.root is None:
16+
print("List is empty")
17+
return
18+
else:
19+
print("List is: ")
20+
p=self.root
21+
while p is not None:
22+
print(p.data," ",end=" ")
23+
p=p.link
24+
print()
25+
26+
def insertAtBeg(self, data):
27+
temp=Node(data)
28+
temp.link=self.root
29+
self.root=temp
30+
31+
def insertAtEnd (self,data):
32+
temp=Node(data)
33+
if self.root is None:
34+
self.root=temp
35+
return
36+
p=self.root
37+
while p.link is not None:
38+
p= p.link
39+
p.link=temp
40+
41+
def creatList(self):
42+
n=int(input("Enter the No of elements you want: "))
43+
if n==0:
44+
return
45+
for i in range(n):
46+
data=int(input("Enter elemenets in the List: "))
47+
self.insertAtEnd(data)
48+
49+
def concatelist(self,list2):
50+
if self.root is None: #if list 1 is empty
51+
self.root=list2.root #add root of 1st list to another list
52+
return
53+
if list2.root is None: #if 2nd list is empty return
54+
return
55+
p=self.root #when there are 2 list
56+
while p.link is not None:
57+
p=p.link #finding reference to last node of 1st list
58+
p.link=list2.root #connecting last not of 1st list to 1st node of 2nd list.(Connecting at end of 1st list)
59+
60+
list1=singleLinkedList()
61+
list2=singleLinkedList()
62+
63+
print("Enter 1st list: ")
64+
list1.creatList()
65+
print("Enter 2nd list: ")
66+
list2.creatList()
67+
68+
print("1st list: ")
69+
list1.displayList()
70+
print("2nd list: ")
71+
list2.displayList()
72+
73+
list1.concatelist(list2)
74+
print("Concatinated list: ")
75+
list1.displayList()
76+
77+
78+
# CONCATENATION OF LINKED LIST
79+
# B.CIRCULAR LINKED LIST
80+
81+
class Node:
82+
def __init__(self,value):
83+
self.data=value
84+
self.link=None
85+
86+
class CircularLinkedList:
87+
def __init__(self):
88+
self.last=None
89+
90+
def displaylist(self):
91+
if self.last is None:
92+
print("List is empty!!")
93+
return
94+
p=self.last.link
95+
while True:
96+
print(p.data,end=" ")
97+
p=p.link
98+
if p==self.last.link:
99+
break
100+
print()
101+
102+
def insertatend(self,data):
103+
temp=Node(data)
104+
temp.link=self.last.link
105+
self.last.link=temp
106+
self.last=temp
107+
108+
def insertatempty(self,data):
109+
temp=Node(data)
110+
self.last=temp
111+
self.last.link=self.last
112+
113+
def createlist(self):
114+
n=int(input("Enter the no.of elements in list: "))
115+
if n==0:
116+
return
117+
data=int(input("Enter the elements of list: "))
118+
self.insertatempty(data)
119+
for i in range(n-1):
120+
data = int(input("Enter the elements of list: "))
121+
self.insertatend(data)
122+
123+
def concatelist(self,list2):
124+
if self.last is None:
125+
self.last=list2.last
126+
return
127+
if list2.last is None:
128+
return
129+
p=self.last.link
130+
self.last.link=list2.last.link
131+
list2.last.link=p
132+
self.last=list2.last
133+
134+
135+
list1=CircularLinkedList()
136+
list2=CircularLinkedList()
137+
138+
print("Enter 1st list: ")
139+
list1.createlist()
140+
print("Enter 2nd list: ")
141+
list2.createlist()
142+
143+
print("1st list: ")
144+
list1.displaylist()
145+
print("2nd list: ")
146+
list2.displaylist()
147+
148+
list1.concatelist(list2)
149+
print("Concatinated list: ")
150+
list1.displaylist()

0 commit comments

Comments
 (0)