-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularDoubleLinkedList.py
169 lines (155 loc) · 4.51 KB
/
CircularDoubleLinkedList.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
162
163
164
165
166
167
168
169
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class CircularDoubleLinkedList:
def __init__(self):
self.head = None
def printLL(self):
if self.head is None:
print("List is empty")
else:
n = self.head
while True:
print(n.data, "<-->", end=" ")
n = n.next
if n == self.head:
break
print("HEAD")
def printLL_reverse(self):
if self.head is None:
print("List is empty")
else:
n = self.head
while n.next != self.head: # Move to the last node
n = n.next
last = n
while True:
print(n.data, "<-->", end=" ")
n = n.prev
if n == last:
break
print("HEAD")
def add_begin(self, item):
new_node = Node(item)
if self.head is None:
self.head = new_node
new_node.next = new_node
new_node.prev = new_node
else:
last = self.head.prev
new_node.next = self.head
new_node.prev = last
last.next = new_node
self.head.prev = new_node
self.head = new_node
def add_end(self, item):
new_node = Node(item)
if self.head is None:
self.head = new_node
new_node.next = new_node
new_node.prev = new_node
else:
last = self.head.prev
last.next = new_node
new_node.prev = last
new_node.next = self.head
self.head.prev = new_node
def add_before(self, item, x):
if self.head is None:
print("List is empty")
return
n = self.head
while True:
if n.data == x:
break
n = n.next
if n == self.head:
print("Node not found")
return
new_node = Node(item)
new_node.next = n
new_node.prev = n.prev
n.prev.next = new_node
n.prev = new_node
if n == self.head: # Update head if needed
self.head = new_node
def add_after(self, item, x):
if self.head is None:
print("List is empty")
return
n = self.head
while True:
if n.data == x:
break
n = n.next
if n == self.head:
print("Node not found")
return
new_node = Node(item)
new_node.prev = n
new_node.next = n.next
n.next.prev = new_node
n.next = new_node
def delete_begin(self):
if self.head is None:
print("List is empty")
elif self.head.next == self.head: # Single node case
self.head = None
else:
last = self.head.prev
self.head = self.head.next
self.head.prev = last
last.next = self.head
def delete_end(self):
if self.head is None:
print("List is empty")
elif self.head.next == self.head: # Single node case
self.head = None
else:
last = self.head.prev
second_last = last.prev
second_last.next = self.head
self.head.prev = second_last
def delete_value(self, x):
if self.head is None:
print("List is empty")
return
n = self.head
while True:
if n.data == x:
break
n = n.next
if n == self.head:
print("Node not found")
return
if n.next == n: # Single node case
self.head = None
else:
n.prev.next = n.next
n.next.prev = n.prev
if n == self.head: # Update head if needed
self.head = n.next
CDLL = CircularDoubleLinkedList()
CDLL.add_begin(10)
CDLL.printLL()
CDLL.add_begin(20)
CDLL.printLL()
CDLL.add_begin(30)
CDLL.printLL()
CDLL.add_end(40)
CDLL.printLL()
CDLL.add_before(5, 40)
CDLL.printLL()
CDLL.add_after(31, 10)
CDLL.printLL()
CDLL.add_after(10, 30)
CDLL.printLL()
CDLL.delete_begin()
CDLL.printLL()
CDLL.delete_end()
CDLL.printLL()
CDLL.delete_value(10)
CDLL.printLL()
CDLL.printLL_reverse()