-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublycircular.py
139 lines (119 loc) · 3.71 KB
/
doublycircular.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
#Creating the node
class Node:
def __init__(self,item=None, prev=None,next=None):
self.item = item
self.prev = prev
self.next = next
#Creating the CDLL
class CDLL:
def __init__(self, start=None):
self.start = start
def is_empty(self):
return self.start is None
def insert_at_start(self, data):
new_node = Node(data)
if self.is_empty():
new_node.next = new_node
new_node.prev = new_node
else:
new_node.next = self.start
new_node.prev = self.start.prev
self.start.prev.next = new_node
self.start.prev = new_node
self.start = new_node
def insert_at_last(self, data):
new_node = Node(data)
if self.is_empty():
new_node.next = new_node
new_node.prev = new_node
self.start = new_node
else:
new_node.next = self.start
new_node.prev = self.start.prev
new_node.prev.next = new_node
self.start.prev = new_node
def search(self, data):
temp = self.start
if temp == None:
return None
if temp.item == data:
return temp
else:
temp = temp.next
while temp != self.start:
if temp.item == data:
return temp
temp = temp.next
return None
def insert_after(self, temp, data):
if temp is not None:
new_node = Node(data)
new_node.next = temp.next
new_node.prev = temp
temp.next.prev = new_node
temp.next = new_node
def print_list(self):
temp = self.start
if temp is not None:
print(temp.item, end=' ')
temp = temp.next
while temp is not self.start:
print(temp.item, end=' ')
temp = temp.next
def delete_first(self):
if self.start is not None:
if self.start.next == self.start:
self.start = None
else:
self.start.prev.next = self.start.next
self.start.next.prev = self.start.prev
self.start = self.start.next
def delete_last(self):
if self.start is not None:
if self.start.next == self.start:
self.start = None
else:
self.start.prev.prev.next = self.start
self.start.prev = self.start.prev.prev
def delete_item(self, data):
if self.start is not None:
temp = self.start
if temp.item == data:
self.delete_first()
else:
temp = temp.next
while temp != self.start:
if temp.item == data:
temp.next.prev = temp.prev
temp.prev.next = temp.next
temp = temp.next
def __iter__(self):
return CDLLIterator(self.start)
class CDLLIterator:
def __init__(self, start):
self.current = start
self.start = start
self.count = 0
def __iter__(self):
return self
def __next__(self):
if self.current is None:
raise StopIteration
if self.current == self.start and self.count == 1:
raise StopIteration
else:
self.count = 1
data = self.current.item
self.current = self.current.next
return data
mylist = CDLL()
mylist.insert_at_start(10)
mylist.insert_at_last(20)
mylist.insert_at_last(30)
mylist.insert_at_last(66)
mylist.insert_after(mylist.search(20), 25)
mylist.print_list()
print()
for x in mylist:
print(x, end=' ')
print()