-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedList.py
63 lines (52 loc) · 1.86 KB
/
DoublyLinkedList.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList:
def __init__(self):
self.tail = None
self.head = None
self.size = 0
def delete_node(self, node_to_delete):
# case: empty list
if self.head is None or node_to_delete is None:
return
# case: we want to delete the only node
if self.head == node_to_delete and self.tail == node_to_delete:
self.tail = None
self.head = None
else:
# case: the node is the last one
if self.tail == node_to_delete:
node_to_delete.prev.next = None
self.tail = node_to_delete.prev
# case: the node is the first one
if self.head == node_to_delete:
node_to_delete.next.prev = None
self.head = node_to_delete.next
# case: normal
# modify the node's next ref
if node_to_delete.next != None and node_to_delete.prev != None:
node_to_delete.next.prev = node_to_delete.prev
# case: normal
# modify the node's prev ref
if node_to_delete.prev != None and node_to_delete.next != None:
node_to_delete.prev.next = node_to_delete.next
self.size -= 1
def push(self, new_data):
new_node = Node(new_data)
new_node.prev = self.tail
# case: adding the first element
if self.head == None:
self.head = new_node
self.tail = new_node
if self.tail != None:
self.tail.next = new_node
self.tail = new_node
self.size += 1
def print_dll(self):
curr_node = self.head
while curr_node != None:
print(f'None: data-> {curr_node.data}')
curr_node = curr_node.next