Skip to content

Commit 0c9be0e

Browse files
authored
Merge pull request #105 from aarjavpatni/circular-linked-list
Circular Linked List
2 parents 552b505 + f2115fa commit 0c9be0e

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

DSA/circularLinkedList.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
6+
class CircularLinkedList:
7+
def __init__(self):
8+
self.last = None
9+
10+
def addToEmpty(self, data):
11+
12+
if self.last != None:
13+
return self.last
14+
15+
# allocate memory to the new node and add data to the node
16+
newNode = Node(data)
17+
18+
# assign last to newNode
19+
self.last = newNode
20+
21+
# create link to iteself
22+
self.last.next = self.last
23+
return self.last
24+
25+
def addFront(self, data):
26+
27+
# check if the list is empty
28+
if self.last == None:
29+
return self.addToEmpty(data)
30+
31+
# allocate memory to the new node and add data to the node
32+
newNode = Node(data)
33+
34+
# store the address of the current first node in the newNode
35+
newNode.next = self.last.next
36+
37+
# make newNode as last
38+
self.last.next = newNode
39+
40+
return self.last
41+
42+
def addEnd(self, data):
43+
# check if the node is empty
44+
if self.last == None:
45+
return self.addToEmpty(data)
46+
47+
# allocate memory to the new node and add data to the node
48+
newNode = Node(data)
49+
50+
# store the address of the last node to next of newNode
51+
newNode.next = self.last.next
52+
53+
# point the current last node to the newNode
54+
self.last.next = newNode
55+
56+
# make newNode as the last node
57+
self.last = newNode
58+
59+
return self.last
60+
61+
def addAfter(self, data, item):
62+
63+
# check if the list is empty
64+
if self.last == None:
65+
return None
66+
67+
newNode = Node(data)
68+
p = self.last.next
69+
while p:
70+
71+
# if the item is found, place newNode after it
72+
if p.data == item:
73+
74+
# make the next of the current node as the next of newNode
75+
newNode.next = p.next
76+
77+
# put newNode to the next of p
78+
p.next = newNode
79+
80+
if p == self.last:
81+
self.last = newNode
82+
return self.last
83+
else:
84+
return self.last
85+
p = p.next
86+
if p == self.last.next:
87+
print(item, "The given node is not present in the list")
88+
break
89+
90+
def deleteNode(self, last, key):
91+
92+
# If linked list is empty
93+
if last == None:
94+
return
95+
96+
# If the list contains only a single node
97+
if (last).data == key and (last).next == last:
98+
99+
last = None
100+
101+
temp = last
102+
d = None
103+
104+
# if last node is to be deleted
105+
if (last).data == key:
106+
107+
# find the node before the last node
108+
while temp.next != last:
109+
temp = temp.next
110+
111+
# point temp node to the next of last i.e. first node
112+
temp.next = (last).next
113+
last = temp.next
114+
115+
# travel to the node to be deleted
116+
while temp.next != last and temp.next.data != key:
117+
temp = temp.next
118+
119+
# if node to be deleted was found
120+
if temp.next.data == key:
121+
d = temp.next
122+
temp.next = d.next
123+
124+
return last
125+
126+
def traverse(self):
127+
if self.last == None:
128+
print("The list is empty")
129+
return
130+
131+
newNode = self.last.next
132+
while newNode:
133+
print(newNode.data, end=" ")
134+
newNode = newNode.next
135+
if newNode == self.last.next:
136+
break
137+
138+
if __name__ == "__main__":
139+
140+
cll = CircularLinkedList()
141+
142+
last = cll.addToEmpty(6)
143+
last = cll.addEnd(8)
144+
last = cll.addFront(2)
145+
last = cll.addAfter(10, 2)
146+
147+
cll.traverse()
148+
149+
last = cll.deleteNode(last, 8)
150+
print()
151+
cll.traverse()

0 commit comments

Comments
 (0)