-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cll_doubly_list.py
163 lines (141 loc) · 3.91 KB
/
Cll_doubly_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
162
163
class Node:
def __init__(self,item=None,prev=None,next=None):
self.prev=prev
self.item=item
self.next=next
class CDLL:
def __init__(self,start=None):
self.start=start
def is_empty(self):
return self.start==None
def insert_at_start(self,data):
n=Node(data)
if not self.is_empty():
# if self.start.next==self.start.prev: #checcking if only single node existing in linked list
n.prev=self.start.prev
n.next=self.start
self.start.prev.next=n
self.start.prev=n
self.start=n
else:
n.prev=n
n.next=n
self.start=n
def insert_at_last(self,data):
n=Node(data)
if not self.is_empty():
n.prev=self.start.prev
n.next=self.start
self.start.prev.next=n
self.start.prev=n
else:
n.prev=n
n.next=n
self.start=n
def serach(self,data):
if not self.is_empty():
temp=self.start
while temp.next!=self.start:
if temp.item==data:
return temp
else:
temp=temp.next
if temp.item==data:
return temp
def insert_after_node(self,temp,data):
if temp is not None:
n=Node(data)
n.prev=temp
n.next=temp.next
temp.next.prev=n
temp.next=n
else:
return "item not found in the list"
def delete_first(self):
if not self.is_empty():
if self.start.next==self.start:
self.start=None
else:
self.start.next.prev=self.start.prev
self.start.prev.next=self.start.next
self.start=self.start.next
def delete_last(self):
if not self.is_empty():
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 not self.is_empty():
temp=self.serach(data)
if temp is not None:
temp.prev.next=temp.next
temp.next.prev=temp.prev
return None
return "item not found"
return "linked list is empty"
def printlist(self):
temp=self.start
while temp.next!=self.start:
print(temp.item,end=' ')
temp=temp.next
print(temp.item,end=' ')
def __iter__(self):
return CDLLiterator(self.start)
class CDLLiterator:
def __init__(self,start):
self.current=start
self.first=start
self.count=0
def __iter__(self):
return self
def __next__(self):
if self.current is None:
raise StopIteration
if self.current==self.first 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_start(20)
mylist.insert_at_start(30)
mylist.printlist()
print()
mylist.insert_at_last(40)
mylist.printlist()
mylist.insert_at_last(80)
print()
mylist.printlist()
print()
# print(mylist.serach(30))
print(mylist.insert_after_node(mylist.serach(80),60))
mylist.printlist()
# mylist.delete_first()
# print()
# print('-----')
# mylist.printlist()
# mylist.delete_first()
# print()
# print('-----')
# mylist.printlist()
# mylist.delete_first()
# print()
# print('-----')
# mylist.printlist()
# mylist.delete_first()
# print()
# print('-----')
# mylist.printlist()
print(mylist.delete_item(20))
print()
print("--------")
mylist.printlist()
print()
print("==========")
for x in mylist:
print(x,end=' ')