-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_linked_list.py
More file actions
114 lines (97 loc) · 3.11 KB
/
double_linked_list.py
File metadata and controls
114 lines (97 loc) · 3.11 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
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def insert_head(self, node: Node):
""" insert node at the head of the list"""
if self.head is None:
self.head = node
self.tail = node
else:
node.next = self.head
self.head.prev = node
self.head = node
def append(self, node: Node):
""" insert node at the end of the list"""
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
node.prev = self.tail
self.tail = node
# cursor = self.head
# while cursor.next is not None:
# cursor = cursor.next
# cursor.next = node
def delete_head(self):
""" deletes node at head of list """
if self.head is None:
return
else:
self.head = self.head.next
def delete_tail(self):
""" deletes last node """
cursor = self.head
previous_cursor = self.head
while cursor.next is not None:
previous_cursor = cursor
cursor = cursor.next
# print(cursor.data)
previous_cursor.next = None
return cursor
# print(f"previous cursor is {previous_cursor.data}")
def find_node(self, data):
""" finds node corresponding to given data """
if self.head is None:
print("list is empty")
return None
cursor = self.head
while cursor.data is not data and cursor.next is not None:
cursor = cursor.next
if cursor.next is None:
return None
else:
return cursor
def insert_node_after(self, node: Node, after_this: Node):
""" insert new node after given node """
node.next = after_this.next
after_this.next.prev = node
after_this.next = node
node.prev = after_this
def insert_node_before(self, node: Node, before_this: Node):
node.next = before_this
node.prev = before_this.prev
node.prev.next = node
before_this.prev = node
def print_list_forward(self):
""" prints all elements of list """
cursor = self.head
while cursor is not None:
print(f"[{cursor.data}]->", end="")
cursor = cursor.next
def print_list_reverse(self):
cursor = self.tail
# handle empty list
if cursor is None:
return
# handle list with 1 element
if cursor is self.head:
print(f"<-[{cursor.data}]",end="")
return
while cursor is not None:
print(f"<-[{cursor.data}]",end="")
cursor = cursor.prev
def size(self):
""" prints length of the list """
counter = 0
cursor = self.head
while cursor is not None:
cursor = cursor.next
counter = counter + 1
return counter