Skip to content

Commit a503781

Browse files
authored
Single linked list code.
1 parent a21f801 commit a503781

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed

Single Linked List

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,242 @@
1+
##code to implement linked list using Python.
2+
class Node:
3+
def __init__(self,value): #initialising the node |data_|link_|
4+
self.data=value
5+
self.link=None
16

7+
class singleLinkedList:
8+
def __init__(self): #initialising the root element
9+
self.root=None
10+
11+
def displayList(self): #for displaying the list elements
12+
if self.root is None:
13+
print("List is empty")
14+
return
15+
else:
16+
print("List is: ")
17+
p=self.root
18+
while p is not None:
19+
print(p.data," ",end=" ")
20+
p=p.link
21+
print()
22+
def countNode(self):
23+
count=0
24+
p=self.root
25+
while p is not None:
26+
count+=1
27+
p=p.link
28+
print("No.of nodes in list are:",count)
29+
30+
def searchNode(self,ele):
31+
loc=1
32+
p=self.root
33+
while p is not None:
34+
if(p.data==ele):
35+
print(ele,"Is found in list at",loc)
36+
return True
37+
loc+=1
38+
p=p.link
39+
else:
40+
print(ele,"Is not found in the list!")
41+
return False
42+
43+
def insertAtBeg(self,data):
44+
temp=Node(data)
45+
temp.link=self.root
46+
self.root=temp
47+
48+
def insertAtEnd (self,data):
49+
temp=Node(data)
50+
if self.root is None:
51+
self.root=temp
52+
return
53+
p=self.root
54+
while p.link is not None:
55+
p= p.link
56+
p.link=temp
57+
58+
def creatList(self):
59+
n=int(input("Enter the No of elements you want: "))
60+
if n==0:
61+
return
62+
for i in range(n):
63+
data=int(input("Enter elemenets in the List: "))
64+
self.insertAtEnd(data)
65+
66+
def insertAfterSpecified(self,data,loc):
67+
p=self.root
68+
while p is not None:
69+
if p.data==loc:
70+
break
71+
p=p.link
72+
if p is not None:
73+
temp = Node(data)
74+
temp.link = p.link
75+
p.link = temp
76+
77+
def insertBeforeSpecified(self,data,loc):
78+
if self.root is None:
79+
print("List is empty.")
80+
return
81+
if loc==self.root.data:
82+
temp=Node(data)
83+
temp.link=self.root
84+
self.root=temp
85+
return
86+
p=self.root
87+
while p.link is not Node:
88+
if p.link.data==loc:
89+
break
90+
p=p.link
91+
if p.link is None:
92+
print(loc,"Not present in this list.")
93+
else:
94+
temp=Node(data)
95+
temp.link=p.link
96+
p.link=temp
97+
98+
def insertAtPosition(self,data,loc):
99+
if loc==1:
100+
temp=Node(data)
101+
temp.link=self.root
102+
self.root=temp
103+
return
104+
105+
p=self.root
106+
i=1
107+
while i<loc-1 and p is not None:
108+
p=p.link
109+
i+=1
110+
111+
if p is None:
112+
print("You can only insert till",i)
113+
else:
114+
temp=Node(data)
115+
temp.link=p.link
116+
p.link=temp
117+
118+
119+
def deleteFirst(self):
120+
pass
121+
122+
def deleteLast(self):
123+
pass
124+
125+
def deleteAny(self):
126+
pass
127+
128+
def reverseList(self):
129+
pass
130+
131+
def bsortByData(self):
132+
pass
133+
134+
def bsortByLink(self):
135+
pass
136+
137+
def hasCycle(self):
138+
pass
139+
140+
def insertCycle(self):
141+
pass
142+
143+
def detectCycle(self):
144+
pass
145+
146+
def removeCycle(self):
147+
pass
148+
149+
def merge2(self):
150+
pass
151+
152+
def merge2(self):
153+
pass
154+
155+
def mergeSort(self):
156+
pass
157+
158+
def mergesort2(self):
159+
pass
160+
161+
def divideList(self):
162+
pass
163+
164+
list=singleLinkedList()
165+
list.creatList()
166+
167+
while True:
168+
print("1.Display List")
169+
print("2.Count the no.of nodes")
170+
print("3.Search a node")
171+
print("4.Insert in empty list/insert at beginning")
172+
print("5.Insert at end of list")
173+
print("6.Insert after a specified node")
174+
print("7.Insert before a specified node")
175+
print("8. Insert at given position")
176+
print("9.Delete first node")
177+
print("10.Delete last node")
178+
print("11.Delete any node")
179+
print("12.Reverse the list")
180+
print("13.Bubble sort by exchaning Data")
181+
print("14.Bubble sort by exchaning links")
182+
print("15.Merge sort")
183+
print("16.Insert Cycle")
184+
print("17.Detect Cycle")
185+
print("18.Remove Cycle")
186+
print("19.Quit")
187+
188+
choice=int(input("Enter a choice/operation you want to perform:"))
189+
if choice==1:
190+
list.displayList()
191+
elif choice==2:
192+
list.countNode()
193+
elif choice==3:
194+
ele=int(input("Enter the element to be searched: "))
195+
list.searchNode(ele)
196+
elif choice==4:
197+
data=int(input("Enter the data to be inserted: "))
198+
list.insertAtBeg(data)
199+
elif choice==5:
200+
data = int(input("Enter the data to be inserted: "))
201+
list.insertAtEnd(data)
202+
elif choice==6:
203+
data = int(input("Enter the data to be inserted: "))
204+
loc=int(input("Enter the Location at which data to be inserted: "))
205+
list.insertAfterSpecified(data,loc)
206+
elif choice==7:
207+
data = int(input("Enter the data to be inserted: "))
208+
loc = int(input("Enter the Location at which data to be inserted: "))
209+
list.insertBeforeSpecified(data,loc)
210+
elif choice==8:
211+
data = int(input("Enter the data to be inserted: "))
212+
loc = int(input("Enter the Location at which data to be inserted: "))
213+
list.insertAtPosition(data,loc)
214+
elif choice==9:
215+
list.deleteFirst()
216+
elif choice==10:
217+
list.deleteLast()
218+
elif choice==11:
219+
data = int(input("Enter the data to be deleted: "))
220+
list.deleteAny(data)
221+
elif choice==12:
222+
list.reverseList()
223+
elif choice==13:
224+
list.bsortByData()
225+
elif choice==14:
226+
list.bsortByLink()
227+
elif choice==15:
228+
list.mergeSort()
229+
elif choice==16:
230+
data = int(input("Enter the data at which the cycle has to be inserted: "))
231+
list.insertCycle(data)
232+
elif choice==17:
233+
if(list.hasCycle()):
234+
print("List has a cycle..")
235+
else:
236+
print("List doesnt have a cycle.")
237+
elif choice==18:
238+
list.removeCycle()
239+
elif choice==19:
240+
break
241+
else:
242+
print("Invalid choice!")

0 commit comments

Comments
 (0)