-
Notifications
You must be signed in to change notification settings - Fork 65
/
circular_linked_list.py
118 lines (100 loc) · 3.26 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
""" Implementation of Circular Linked List Data Structure
Circular linked list is a linked list where all nodes are connected to
form a circle. There is no NULL at the end. A circular linked list can
be a singly circular linked list or doubly circular linked list.
"""
class Node:
""" Node class contains everything related to Linked List node """
def __init__(self, data):
""" initializing single node with data """
self.data = data
self.next = None
class CircularLinkedList:
""" Circular linked list is a linked list where all nodes are connected
to form a circle.
"""
def __init__(self):
""" initializing circular linked list with zero node """
self.head = None
def __len__(self):
""" get number of nodes currently present in circular linked list """
if self.head is None:
return 0
current = self.head
count = 1
while current.next is not self.head:
count += 1
current = current.next
return count
def insert_head(self, data):
""" inserts node at the start of linked list """
if self.head is None:
self.head = Node(data)
self.head.next = self.head
return
new_node = Node(data)
current = self.head
while current.next is not self.head:
current = current.next
current.next = new_node
new_node.next = self.head
self.head = new_node
def insert_tail(self, data):
""" inserts node at the end of linked list """
if self.head is None:
self.head = Node(data)
self.head.next = self.head
return
new_node = Node(data)
current = self.head
while current.next is not self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def remove(self, key):
""" remove data key from circular linked list """
if self.head.data == key:
previous = self.head
while previous.next is not self.head:
previous = previous.next
previous.next = self.head.next
self.head = previous.next
return
current = self.head.next
previous = self.head
while current is not self.head:
if current.data == key:
break
previous = current
current = current.next
previous.next = current.next
def print(self):
""" prints entire linked list without changing underlying data """
current = self.head
while current is not None:
print(" ->", current.data, end="")
current = current.next
if current == self.head:
break
print(end=" -> ...")
print()
def main():
""" operational function """
cll = CircularLinkedList()
cll.insert_tail('C')
cll.print()
cll.insert_tail('D')
cll.print()
cll.insert_head('B')
cll.print()
cll.insert_head('A')
cll.print()
print("size:", len(cll))
cll.remove('B')
cll.print()
print("size:", len(cll))
cll.remove('A')
cll.print()
print("size:", len(cll))
if __name__ == '__main__':
main()