-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublyLL_addAfter_addBefore.py
132 lines (119 loc) · 3.91 KB
/
doublyLL_addAfter_addBefore.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
#Resources:
#https://www.youtube.com/watch?v=dPGBKZBYy0w&list=PL5tcWHG-UPH3nDinW5u_oRcNv6hwhY7ET&index=2&ab_channel=LucidProgramming
class Node:
def __init__(self,data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
# if ll is empty
if self.head is None:
self.head = Node(data)
self.head.next = None
else:
#traverse to the end of the list
currPtr = self.head
while currPtr.next:
currPtr = currPtr.next
# create new node
newNode = Node(data)
currPtr.next = newNode
newNode.prev = currPtr
newNode.next = None
def prepend(self, data):
# if ll is empty
if self.head is None:
self.head = Node(data)
else:
currPtr = self.head
#create new node
newNode = Node(data)
newNode.next = currPtr
newNode.prev = None
self.head = newNode
def printLL(self):
if self.head is None:
print("nothing to print here")
else:
currPtr = self.head
while currPtr:
print(currPtr.data, "<->", end="")
currPtr = currPtr.next
print()
def addAfterNode(self,key,data):
# steps
# traverse to the node that you want to add after
# store the details of the curr node, the next node as well as new node
# create new node
newNode = Node(data)
#traverse to the node where you want to append the newNode
if self.head is None:
self.head = newNode
else:
currPtr = self.head
while currPtr.next:
currPtr = currPtr.next
if currPtr.data == key:
#store the value of pushedNode
pushedNode = currPtr.next
#start the transfer of pointers
currPtr.next = newNode
newNode.prev = currPtr
newNode.next = pushedNode
pushedNode.prev = newNode
print("succeeded squishing process on the right of key")
return
return
def AddBeforeNode(self,key,data):
#traverse to the node where you want to append the newNode
if self.head is None:
self.head = Node(data)
# if you have to add the newnode before the head node
elif self.head.next is None:
newNode = Node(data)
newNode.next = self.head
self.head.prev = newNode
newNode.prev = None
self.head = newNode
else:
currPtr = self.head
while currPtr.next:
currPtr = currPtr.next
if currPtr.data == key:
newNode = Node(data)
prevNode = currPtr.prev
prevNode.next = newNode
newNode.prev = prevNode
newNode.next = currPtr
currPtr.prev = newNode
print("squishing succeeded on the left of key")
return
return
# newLL = DoublyLinkedList()
# newLL.prepend(0)
# newLL.printLL()
# newLL.AddBeforeNode(0,-1)
# newLL.append(1)
# newLL.printLL()
# newLL.append(2)
# newLL.printLL()
# newLL.prepend(-1)
# newLL.printLL()
# newLL.append(4)
# newLL.printLL()
# newLL.AddBeforeNode(4,3.5)
# newLL.printLL()
# newLL.addAfterNode(2,3)
# newLL.printLL()
# 0 <->
# -1 <->0 <->1 <->
# -1 <->0 <->1 <->2 <->
# -1 <->-1 <->0 <->1 <->2 <->
# -1 <->-1 <->0 <->1 <->2 <->4 <->
# squishing succeeded on the left of key
# -1 <->-1 <->0 <->1 <->2 <->3.5 <->4 <->
# succeeded squishing process on the right of key
# -1 <->-1 <->0 <->1 <->2 <->3 <->3.5 <->4 <->