-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularDoublyLinkedList.py
159 lines (142 loc) · 4.74 KB
/
circularDoublyLinkedList.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
# Circular Doubly Linked List implementation
# Create, Insert, Traverse, Search, Delete operations
class Node:
def __init__(self, value=None):
self.value = value
self.next = None
self.prev = None
class CircularDoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def __iter__(self):
node = self.head
while node:
yield node
node = node.next
if node == self.tail.next:
break
# Create
def createCircularDoublyLL(self, nodeValue):
newNode = Node(nodeValue)
self.head = newNode
self.tail = newNode
newNode.prev = newNode
newNode.next = newNode
return "The Circular Doubly LL is created"
# Insert
def insertCircularDoublyLL(self, value, location):
if self.head is None:
return "The Circular Doubly LL does not exist"
else:
newNode = Node(value)
# Beginning
if location == 0:
newNode.next = self.head
newNode.prev = self.tail
self.head.prev = newNode
self.head = newNode
self.tail.next = newNode
# End
elif location == 1:
newNode.next = self.head
newNode.prev = self.tail
self.head.prev = newNode
self.tail.next = newNode
self.tail = newNode
# Middle
else:
tempNode = self.head
index = 0
while index < location - 1:
tempNode = tempNode.next
index += 1
newNode.next = tempNode.next
newNode.prev = tempNode
newNode.next.prev = newNode
tempNode.next = newNode
return "The node has been inserted"
# Traverse
def traverseCircularDoublyLL(self):
if self.head is None:
print("There is not any node")
else:
tempNode = self.head
while tempNode:
print(tempNode.value)
if tempNode == self.tail:
break
tempNode = tempNode.next
# Reverse traverse
def reverseTraverseCircularDoublyLL(self):
if self.head is None:
print("There is not any node")
else:
tempNode = self.tail
while tempNode:
print(tempNode.value)
if tempNode == self.head:
break
tempNode = tempNode.prev
# Search
def searchCircularDoublyLL(self, nodeValue):
if self.head is None:
return "There is not any node"
else:
tempNode = self.head
while tempNode:
if tempNode.value == nodeValue:
return tempNode.value
if tempNode == self.tail:
return "The value does not exist"
tempNode = tempNode.next
# Delete
def deleteNode(self, location):
if self.head is None:
print("There is not any node")
else:
# Beginning
if location == 0:
if self.head == self.tail:
self.head.prev = None
self.head.next = None
self.head = None
self.tail = None
else:
self.head = self.head.next
self.head.prev = self.tail
self.tail.next = self.head
# End
elif location == 1:
if self.head == self.tail:
self.head.prev = None
self.head.next = None
self.head = None
self.tail = None
else:
self.tail = self.tail.prev
self.tail.next = self.head
self.head.prev = self.tail
# Middle
else:
curNode = self.head
index = 0
while index < location - 1:
curNode = curNode.next
index += 1
curNode.next = curNode.next.next
curNode.next.prev = curNode
print("The node has been deleted")
# Delete Entire
def deleteEntireCircularDoublyLL(self):
if self.head is None:
print("There is not any node")
else:
self.tail.next = None
tempNode = self.head
while tempNode:
tempNode.prev = None
tempNode = tempNode.next
self.head = None
self.tail = None
print("The entire list has been deleted")