-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_linkedlist_implementation.py
More file actions
348 lines (295 loc) · 10.3 KB
/
my_linkedlist_implementation.py
File metadata and controls
348 lines (295 loc) · 10.3 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
class SinglyLinkedListNode:
def __init__(self, data):
self.data = data
self.next: SinglyLinkedListNode = None
def __repr__(self) -> str:
return f"<node_data = {self.data}>"
class LinkedListIterator:
def __init__(self, _node: SinglyLinkedListNode):
self.current_node = _node
def __next__(self):
current_node = self.current_node
if current_node is None:
raise StopIteration
next_node = self.current_node.next
self.current_node = next_node
return current_node.data
def current(self):
return self.current_node
class LinkedList:
iter_class = LinkedListIterator
node_class = SinglyLinkedListNode
def __init__(
self,
*args,
unique_data: bool = False,
):
self.head: self.node_class = None
self.tail: self.node_class = None
self.length = 0
self._unique_data = unique_data
if len(args):
print("args:" + str(args))
for data in args:
self.insert_last(data)
self.length = len(args)
self.itr_obj = self.iter_class(self.head)
@property
def unique_data(self):
return self._unique_data
def __iter__(self):
self.itr_obj = self.iter_class(self.head)
return self.itr_obj
def __repr__(self) -> str:
linked_list_repr = ""
for node_data in self:
linked_list_repr += f"({node_data}) "
return f"linked_list -{(str(self.length))}-> |{self.head and self.head.data}| {linked_list_repr} |{self.tail and self.tail.data}|"
def __len__(self):
return self.length
@property
def next_node(self):
itr_obj = self.itr_obj
return itr_obj.current() or self.head
def find_node(self, _data_to_find):
# check if the list is empty
if self.head is None:
return None
current_node = self.head
for node_data in self:
if _data_to_find == node_data:
return current_node
current_node = self.next_node
return None
def is_exists(self, _data_to_find):
# check if the list is empty
if self.head is None:
return False
return _data_to_find in self
def validate_inserted_data(self, _data):
if self._unique_data and self.is_exists(_data):
raise ValueError(f"{_data} already exists in the list")
def insert_last(self, _data):
self.validate_inserted_data(_data)
new_node = self.node_class(_data)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.length += 1
return new_node
def insert_head(self, _data):
self.validate_inserted_data(_data)
new_node = self.node_class(_data)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head = new_node
self.length += 1
return new_node
def insert_after(self, _data_to_insert, _data_to_find):
self.validate_inserted_data(_data_to_insert)
# search about the node contian _data_to_find return error if not found it
ref_node: SinglyLinkedListNode = self.find_node(_data_to_find)
if ref_node is None:
raise ValueError(f"list doesn't contain this value '{_data_to_find}'")
# make new_node contain _data_to_insert
new_node = self.node_class(_data_to_insert)
# make new_node.next be the reference_node.next
new_node.next = ref_node.next
# check if ref_node is the tail
if new_node.next is None:
self.tail = new_node
# make reference_node.next be the new_node
ref_node.next = new_node
# increase the list length
self.length += 1
return new_node
def insert_before(self, _data_to_insert, _data_to_find):
# search about the node contian _data_to_find return error if not found it
# create new_node contain _data_to_insert
# make new_node.next be the ref_node
# check if ref_node was the head make new_node be the head
# find previous_node
# make previous_node.next be the new_node
# increase the list length
self.validate_inserted_data(_data_to_insert)
ref_node: SinglyLinkedListNode = self.find_node(_data_to_find)
if ref_node is None:
raise ValueError(f"list doesn't contain this value '{_data_to_find}'")
new_node = self.node_class(_data_to_insert)
new_node.next = ref_node
if self.head == ref_node:
self.head = new_node
else:
previous_node = self.get_previous_node(ref_node)
if previous_node is not None:
previous_node.next = new_node
self.length += 1
return new_node
def get_previous_node(self, _node: SinglyLinkedListNode):
# validate that _node is not None
# check if the list is empty raise an error
# check if _node is the head then return None
# loop through the list
# return the node if the node.next is _node
# if node doesn't exist raise an error
if _node is None:
raise ValueError(f"node can't be None")
if self.head is None:
raise ValueError(f"the node doesn't exist in the linkedlist")
if self.head == _node:
return None
current_node = self.head
for _ in self:
if current_node.next is _node:
return current_node
current_node = self.next_node
raise ValueError(f"the node doesn't exist in the linkedlist{_node}")
def delete_tail(self):
if self.tail is None:
raise ValueError(f"the linked list is empty")
new_tail = self.get_previous_node(self.tail)
if new_tail is None:
self.head = None
self.tail = None
else:
new_tail.next = None
self.tail = new_tail
self.length -= 1
def delete_head(self):
if self.head is None:
raise ValueError(f"the linked list is empty")
old_head = self.head
self.head = old_head.next
self.length -= 1
return old_head
def delete_node(self, _node: SinglyLinkedListNode):
if self.head is None:
raise ValueError(f"the linked list is empty")
if self.head == _node:
self.delete_head()
elif self.tail == _node:
self.delete_tail()
else:
self.get_previous_node(_node).next = _node.next
del _node
def test_linked_list_structure_and_operations():
passed_test_cases = []
failed_test_cases = []
linked_list = LinkedList(unique_data=True)
# test insert_last
test_1 = "test insert_last"
node_1 = linked_list.insert_last(1)
node_2 = linked_list.insert_last(2)
node_3 = linked_list.insert_last(3)
print(test_1)
if node_1.data == 1 and node_2.data == 2 and node_3.data == 3:
for data, i in zip(linked_list, [1, 2, 3]):
if data != i:
failed_test_cases.append(test_1)
break
passed_test_cases.append(test_1)
print(f"{linked_list } = 1 2 3 ")
# 1 2 3
########################
# test insert_after
test_2 = "test insert_after"
node_4 = linked_list.insert_after(4, 3)
print(test_2)
if node_4.data == 4:
for data, i in zip(linked_list, [1, 2, 3, 4]):
if data != i:
failed_test_cases.append(test_2)
break
passed_test_cases.append(test_2)
print(f"{linked_list } = 1 2 3 4")
# 1 2 3 4
###############
# test insert_before
test_3 = "test insert_before"
node_0 = linked_list.insert_before(0, 1)
print(test_3)
if node_0.data == 0:
for data, i in zip(linked_list, [0, 1, 2, 3, 4]):
if data != i:
failed_test_cases.append(test_3)
break
passed_test_cases.append(test_3)
print(f"{linked_list } = 0 1 2 3 4")
# 0 1 2 3 4
###############
# test find_node
test_4 = "test find_node"
print(test_4)
if linked_list.find_node(3) and linked_list.find_node(3).data == 3:
for data, i in zip(linked_list, [0, 1, 2, 3, 4]):
if data != i:
failed_test_cases.append(test_4)
break
passed_test_cases.append(test_4)
print(f"{linked_list } = 0 1 2 3 4")
# 0 1 2 3 4
###############
# test delete_node
test_5 = "test delete_node"
linked_list.delete_node(node_4)
print(test_5)
if linked_list.find_node(4) is None:
for data, i in zip(linked_list, [0, 1, 2, 3]):
if data != i:
failed_test_cases.append(test_5)
break
passed_test_cases.append(test_5)
print(f"{linked_list } = 0 1 2 3 ")
# 0 1 2 3
###############
# test delete_tail
test_6 = "test delete_tail"
linked_list.delete_tail()
print(test_6)
if linked_list.find_node(3) is None:
for data, i in zip(linked_list, [0, 1, 2]):
if data != i:
failed_test_cases.append(test_6)
break
passed_test_cases.append(test_6)
print(f"{linked_list } = 0 1 2 ")
# 0 1 2
###############
# test delete_head
test_7 = "test delete_head"
linked_list.delete_head()
print(test_7)
if linked_list.find_node(0) is None:
for data, i in zip(linked_list, [1, 2]):
if data != i:
failed_test_cases.append(test_7)
break
passed_test_cases.append(test_7)
print(f"{linked_list } = 1 2 ")
# 1 2
###############
# test unique data
test_8 = "test unique data"
print(test_8)
try:
linked_list.insert_last(2)
except:
passed_test_cases.append(test_8)
finally:
linked_list.insert_after(3, 2)
print(f"{linked_list } = 1 2 3 ")
# 1 2 3
return passed_test_cases, failed_test_cases
if __name__ == "__main__":
passed, failed = test_linked_list_structure_and_operations()
print("\n")
print(f"failed: {failed}")
print("\n")
print(f"passed: {len(passed)}")
print("\n")