-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly_linked_list.py
213 lines (184 loc) · 5.61 KB
/
doubly_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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
class Node:
def __init__(self,item=None,prev=None,next=None):
self.prev=prev
self.item=item
self.next=next
class Dll:
def __init__(self,start=None):
self.start=start
def is_empty(self):
if self.start is None:
return "DLL is empty"
else:
return "DLL is not empty"
def insert_at_start(self,data):
if self.start is not None:
n=Node(data,self.start.prev,self.start)
self.start.prev=n
self.start=n
else:
n=Node(data)
self.start=n
def insert_at_last(self,data):
if self.start is not None:
temp=self.start
n=Node(data)
while temp.next is not None:
temp=temp.next
n.prev=temp
temp.next=n
else:
n=Node(data)
self.start=n
def search(self,data):
if self.start.next is None:
if self.start.item==data:
return f"data {data} is present at {self.start} address/reference"
else:
return f"data {data} is not present in DLL"
elif self.start is None:
return "DLL is empty"
elif self.start is not None:
temp=self.start
while temp is not None:
if temp.item==data:
return f"data {data} is present at {temp} address/reference"
else:
temp=temp.next
return f"data {data} is not present in DLL"
def insert_after(self,data,new_data):
if self.start.next is None:
if self.start.item==data:
n=Node(new_data)
n.prev=self.start
self.start.next=n
else:
return "data is not found"
elif self.start is not None:
temp=self.start
n=Node(new_data)
while temp is not None:
if temp.item==data:
n.prev=temp
n.next=temp.next
temp.next.prev=n
temp.next=n
break
else:
temp=temp.next
def print_Dlist(self):
temp=self.start
while temp is not None:
# print(temp.prev)
# print(temp.item)
# print(temp.next)
# print()
print(temp.item,end=' ')
temp=temp.next
def delete_first(self):
if self.start is not None:
self.start.next.prev=None
self.start=self.start.next
elif self.start.next is None:
self.start=None
else:
return "DLL is empty"
def delete_last(self):
if self.start is None:
return "DLL is empty"
elif self.start is not None:
temp=self.start
while temp.next.next is not None:
temp=temp.next
temp.next=None
else:
self.start=None
def delete_specific_element(self,data):
if self.start is None:
return "DLL is empty"
elif self.start.next is None:
if self.start.item==data:
self.start=None
else:
return "data is not found in DLL"
else:
# print("inside here123")
temp=self.start
# print("first item of DLL ",temp.item)
while temp is not None:
# print(temp.prev)
# print(temp.item)
# print(temp.next)
if temp.item==data and temp.next is not None:
temp.prev.next=temp.next.prev
temp.next.prev=temp.prev.next
break
elif temp.item==data and temp.next is None:
# print("inside this if")
# print(temp.item)
# print("next ",temp.next)
# print("prev " ,temp.prev)
temp.prev.next=None
break
else:
# print("inside here")
temp=temp.next
def __iter__(self):
return DLLiterator(self.start)
class DLLiterator:
def __init__(self,start):
self.current=start
def __iter__(self):
return self
def __next__(self):
if not self.current:
raise StopIteration
data=self.current.item
self.current=self.current.next
return data
myDlist=Dll()
# print(myDlist.is_empty())
myDlist.insert_at_start(10)
# myDlist.print_Dlist()
# print()
print("-------")
myDlist.insert_at_start(20)
myDlist.insert_at_start(30)
myDlist.insert_at_start(40)
# myDlist.print_Dlist()
# print()
for i in myDlist:
print(i,end=' ')
# print(myDlist.is_empty())
# print("-----------")
# myDlist.insert_at_last(90)
# myDlist.print_Dlist()
# # myDlist.insert_at_last(80)
# # myDlist.print_Dlist()
# # print()
# # print(myDlist.search(30))
# # print(myDlist.search(60))
# # print(myDlist.search(90))
# print(myDlist.insert_after(10,120))
# myDlist.print_Dlist()
# print()
# print("deleting the items")
# myDlist.delete_first()
# myDlist.print_Dlist()
# print()
# myDlist.delete_first()
# myDlist.print_Dlist()
# print()
# myDlist.delete_last()
# myDlist.print_Dlist()
# print()
# myDlist.delete_last()
# myDlist.print_Dlist()
# print()
# myDlist.delete_last()
# myDlist.print_Dlist()
# # print()
# print("final check---------")
# # myDlist.print_Dlist()
# myDlist.delete_specific_element(20)
# myDlist.print_Dlist()