forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly_linked_list.py
More file actions
Latest commit
230 lines (198 loc) · 6.56 KB
/
Copy pathdoubly_linked_list.py
File metadata and controls
230 lines (198 loc) · 6.56 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
https://en.wikipedia.org/wiki/Doubly_linked_list
"""
classNode:
def__init__(self, data):
self.data=data
self.previous=None
self.next=None
def__str__(self):
returnf"{self.data}"
classDoublyLinkedList:
def__init__(self):
self.head=None
self.tail=None
def__iter__(self):
"""
>>> linked_list = DoublyLinkedList()
>>> linked_list.insert_at_head('b')
>>> linked_list.insert_at_head('a')
>>> linked_list.insert_at_tail('c')
>>> tuple(linked_list)
('a', 'b', 'c')
"""
node=self.head
whilenode:
yieldnode.data
node=node.next
def__str__(self):
"""
>>> linked_list = DoublyLinkedList()
>>> linked_list.insert_at_tail('a')
>>> linked_list.insert_at_tail('b')
>>> linked_list.insert_at_tail('c')
>>> str(linked_list)
'a->b->c'
"""
return"->".join([str(item) foriteminself])
def__len__(self):
"""
>>> linked_list = DoublyLinkedList()
>>> for i in range(0, 5):
... linked_list.insert_at_nth(i, i + 1)
>>> len(linked_list) == 5
True
"""
returnsum(1for_inself)
definsert_at_head(self, data):
self.insert_at_nth(0, data)
definsert_at_tail(self, data):
self.insert_at_nth(len(self), data)
definsert_at_nth(self, index: int, data):
"""
>>> linked_list = DoublyLinkedList()
>>> linked_list.insert_at_nth(-1, 666)
Traceback (most recent call last):
....
IndexError: list index out of range
>>> linked_list.insert_at_nth(1, 666)
Traceback (most recent call last):
....
IndexError: list index out of range
>>> linked_list.insert_at_nth(0, 2)
>>> linked_list.insert_at_nth(0, 1)
>>> linked_list.insert_at_nth(2, 4)
>>> linked_list.insert_at_nth(2, 3)
>>> str(linked_list)
'1->2->3->4'
>>> linked_list.insert_at_nth(5, 5)
Traceback (most recent call last):
....
IndexError: list index out of range
"""
length=len(self)
ifnot0<=index<=length:
raiseIndexError("list index out of range")
new_node=Node(data)
ifself.headisNone:
self.head=self.tail=new_node
elifindex==0:
self.head.previous=new_node
new_node.next=self.head
self.head=new_node
elifindex==length:
self.tail.next=new_node
new_node.previous=self.tail
self.tail=new_node
else:
temp=self.head
for_inrange(index):
temp=temp.next
temp.previous.next=new_node
new_node.previous=temp.previous
new_node.next=temp
temp.previous=new_node
defdelete_head(self):
returnself.delete_at_nth(0)
defdelete_tail(self):
returnself.delete_at_nth(len(self) -1)
defdelete_at_nth(self, index: int):
"""
>>> linked_list = DoublyLinkedList()
>>> linked_list.delete_at_nth(0)
Traceback (most recent call last):
....
IndexError: list index out of range
>>> for i in range(0, 5):
... linked_list.insert_at_nth(i, i + 1)
>>> linked_list.delete_at_nth(0) == 1
True
>>> linked_list.delete_at_nth(3) == 5
True
>>> linked_list.delete_at_nth(1) == 3
True
>>> str(linked_list)
'2->4'
>>> linked_list.delete_at_nth(2)
Traceback (most recent call last):
....
IndexError: list index out of range
"""
length=len(self)
ifnot0<=index<=length-1:
raiseIndexError("list index out of range")
delete_node=self.head# default first node
iflength==1:
self.head=self.tail=None
elifindex==0:
self.head=self.head.next
self.head.previous=None
elifindex==length-1:
delete_node=self.tail
self.tail=self.tail.previous
self.tail.next=None
else:
temp=self.head
for_inrange(index):
temp=temp.next
delete_node=temp
temp.next.previous=temp.previous
temp.previous.next=temp.next
returndelete_node.data
defdelete(self, data) ->str:
current=self.head
whilecurrent.data!=data: # Find the position to delete
ifcurrent.next:
current=current.next
else: # We have reached the end an no value matches
raiseValueError("No data matching given value")
ifcurrent==self.head:
self.delete_head()
elifcurrent==self.tail:
self.delete_tail()
else: # Before: 1 <--> 2(current) <--> 3
current.previous.next=current.next# 1 --> 3
current.next.previous=current.previous# 1 <--> 3
returndata
defis_empty(self):
"""
>>> linked_list = DoublyLinkedList()
>>> linked_list.is_empty()
True
>>> linked_list.insert_at_tail(1)
>>> linked_list.is_empty()
False
"""
returnlen(self) ==0
deftest_doubly_linked_list() ->None:
"""
>>> test_doubly_linked_list()
"""
linked_list=DoublyLinkedList()
assertlinked_list.is_empty() isTrue
assertstr(linked_list) ==""
try:
linked_list.delete_head()
raiseAssertionError# This should not happen.
exceptIndexError:
assertTrue# This should happen.
try:
linked_list.delete_tail()
raiseAssertionError# This should not happen.
exceptIndexError:
assertTrue# This should happen.
foriinrange(10):
assertlen(linked_list) ==i
linked_list.insert_at_nth(i, i+1)
assertstr(linked_list) =="->".join(str(i) foriinrange(1, 11))
linked_list.insert_at_head(0)
linked_list.insert_at_tail(11)
assertstr(linked_list) =="->".join(str(i) foriinrange(12))
assertlinked_list.delete_head() ==0
assertlinked_list.delete_at_nth(9) ==10
assertlinked_list.delete_tail() ==11
assertlen(linked_list) ==9
assertstr(linked_list) =="->".join(str(i) foriinrange(1, 10))
if__name__=="__main__":
fromdoctestimporttestmod
testmod()