-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularlinkedlist.py
More file actions
105 lines (62 loc) · 1.91 KB
/
Copy pathcircularlinkedlist.py
File metadata and controls
105 lines (62 loc) · 1.91 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class Node:
def __init__(self,data):
self.data=data
self.next=None
class Circular_linkedlist:
def __init__(self):
self.last=None
def addToEmpty(self, data):
if self.last is not None:
return self.last
new_node = Node(data)
self.last = new_node
self.last.next = self.last
return self.last
def add_Front(self,data):
if self.last==None:
return self.addToEmpty(data)
new_node=Node(data)
new_node.next=self.last.next
self.last.next=new_node
return self.last
def add_end(self,data):
if self.last==None:
return self.addToEmpty(data)
new_node=Node(data)
new_node.next=self.last.next
self.last.next=new_node
self.last=new_node
return self.last
def after(self,data,item):
if self.last is None:
return None
p = self.last.next
while True:
if p.data == item:
new_node = Node(data)
new_node.next = p.next
p.next = new_node
if p == self.last:
self.last = new_node
return self.last
p = p.next
if p == self.last.next:
print(item, "The given node is not present in the list")
break
def printlist(self):
if self.last is None:
print("The list is empty")
return
temp = self.last.next
while True:
print(temp.data, end=" ")
temp = temp.next
if temp == self.last.next:
break
if __name__=="__main__":
cll = Circular_linkedlist()
last = cll.addToEmpty(10)
last = cll.add_end(80)
last = cll.add_Front(5)
last = cll.after(15, 10)
cll.printlist()