-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular-linked-list.py
161 lines (133 loc) · 3.5 KB
/
circular-linked-list.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class Node(object):
def __init__(self, value=None, next_node=None):
self.value = value
self.next_node = next_node
def getValue(self):
return self.value
def setValue(self, value):
self.value = value
def getNext(self):
return self.next_node
def setNext(self, next_node):
self.next_node = next_node
class CircularLinkedList:
def __init__(self):
self.head = None
def setHead(self, node):
self.head = node
def getHead(self):
return self.head
def len(self):
head = self.getHead()
current = self.getHead()
counter = 0
while current:
counter += 1
current = current.getNext()
if current == head:
break
return counter
def insert(self, value):
new_node = Node(value)
if self.getHead() is None:
self.setHead(new_node)
new_node.setNext(self.getHead())
current = self.getHead()
while current:
current = current.getNext()
if current.getNext() == self.getHead():
break
current.setNext(new_node)
self.setHead(new_node)
def delete(self, value):
head = self.getHead()
current = self.getHead()
if head.getValue() == value and head == head.getNext():
self.setHead(None)
return True
if head.getValue() == value:
while current.getNext() != head:
current = current.getNext()
current.setNext(head.getNext())
self.setHead(current.getNext())
return True
current = head
while True:
if current.getNext().getValue() == value:
current.setNext(current.getNext().getNext())
return True
current = current.getNext()
if current == head:
break
return False
def search(self, value):
head = self.getHead()
current = head
counter = 0
while current:
counter += 1
if current.getValue() == value:
print(f"Found {value} at {counter}.")
return current
current = current.getNext()
if current == head:
break
return False
def update(self, value, new_value):
head = self.getHead()
current = head
counter = 0
while current:
counter += 1
if current.getValue() == value:
current.setValue(new_value)
print(f"Updated {value} to {new_value} at node {counter}.")
return current
current = current.getNext()
if current == head:
break
return False
def traverse(self):
head = self.getHead()
current = head
counter = 0
while current:
counter += 1
print(f"{current.getValue()}", end=' ')
current = current.getNext()
if current == head:
break
print()
ll = CircularLinkedList()
ll.insert(3)
ll.traverse()
ll.insert(2)
ll.traverse()
ll.insert(1)
ll.traverse()
ll.delete(3)
ll.traverse()
ll.delete(1)
ll.traverse()
ll.delete(2)
ll.traverse()
ll.insert(5)
ll.insert(4)
ll.insert(3)
ll.insert(2)
ll.insert(1)
ll.traverse()
ll.search(4)
ll.update(4, 44)
ll.traverse()
""" Output
3
2 3
1 2 3
1 2
2
1 2 3 4 5
Found 4 at 4.
Updated 4 to 44 at node 4.
1 2 3 44 5
"""