-
Notifications
You must be signed in to change notification settings - Fork 1
/
link.py
executable file
·345 lines (246 loc) · 7.87 KB
/
link.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
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
class Node:
def __init__(self, data=None):
self.next = None
self.prev = None
self.data = data
self.pos = None
def printInfo(self):
# OBJECTIVE: Print all informaiton about this node
print("Node:")
print("\tPos: ", self.pos)
print("\tData: ", self.data)
print("\tAddress: ", self)
print("\tPrev: ", self.prev)
print("\tNext: ", self.next)
class LinkList:
def __init__(self):
# Create a head and tail node
self.head = None
self.tail = None
# Create counter of list size
self.size = 0
def getHead(self):
return self.head
def getTail(self):
return self.tail
def getMiddleNode(self, headNode):
# OBJECTIVE: Get middle position of node from link list
# NOTE: Cannot use self.size because a link list will be divided by half until there is only 1 left,
# so the size will be changing
# Return node if it is empty
if headNode == None:
return headNode
# Set 2 positions
slowNode = headNode
fastNode = headNode
# Iterate link list
# Make sure next node and node after that exist
while (fastNode.next != None) and (fastNode.next.next != None):
# Update nodes
slowNode = slowNode.next
fastNode = fastNode.next.next
# Return slow_node as middle node
return slowNode
def dataAtPosition(self, pos):
# OBJECTIVE: Return node's data at specific position
# Exit if pos is out of range
if pos > self.size or pos <= 0:
print("Out of range!")
return
# Skip to node at specified position
currNode = self.getHead()
for _ in range(pos-1):
currNode = currNode.next
# Return data
return currNode.data
def isEmpty(self):
return self.size == 0
def addHead(self, data):
# OBJECTIVE: If list is empty, immediately add a head
# Create a new node
newNode = Node(data)
# Update head and tail node
self.head = newNode
self.tail = newNode
# Update size
self.size += 1
newNode.pos = self.size
def addNode(self, data):
# OBJECTIVE: Add a new node to the end of the list
# Add head if node is empty
if self.isEmpty():
self.addHead(data)
return
if self.size == 1:
# Create a new node
newNode = Node(data)
# Update head's pointers
self.head.next = newNode
newNode.prev = self.head
# Update tail node
self.tail = newNode
# Update size
self.size += 1
newNode.pos = self.size
return
# Create a new node
newNode = Node(data)
self.tail.next = newNode
newNode.prev = self.tail
self.tail = newNode
self.size += 1
newNode.pos = self.size
def deleteNode(self, pos):
# OBJECTIVE: Delete a node from link list
# Abort if list is empty
if self.isEmpty():
return None
# Get head node
currNode = self.getHead()
# Delete node at Nth position
for _ in range(pos - 1):
currNode = currNode.next
# Relink nodes surrounding currNode
currNode.prev.next = currNode.next
currNode.next.prev = currNode.prev
# Delete currNode
del durrNode
return True
def deleteLinkList(self):
# OBJECTIVE: Delete the entire link list including head and tail nodes
# Get head node
currNode = self.head
while currNode != None:
# Copy node and go to next node
oldNode = currNode
currNode = currNode.next
# Delete oldNode and decrement size
del oldNode
self.size -= 1
return True
def sortedMerge(self, linkListA, linkListB):
# OBJECTIVE: Sort 2 halves of the same link list
newHead = None
# Check if either halves of the list are none
# If data is none, then it must be a tail
if linkListA == None or linkListA.data == None:
return linkListB
if linkListB == None or linkListB.data == None:
return linkListA
# Make a recursive call, divide called link list, and come back here
if linkListA.data <= linkListB.data:
newHead = linkListA
newHead.next = self.sortedMerge(linkListA.next, linkListB)
newHead.next.prev = newHead # Point the next node to newHead
newHead.prev = None
else:
newHead = linkListB
newHead.next = self.sortedMerge(linkListA, linkListB.next)
newHead.next.prev = newHead # Point the next node to newHead
newHead.prev = None
# Return head sorted link list
return newHead
def mergeSort(self, headNode):
# OBJECTIVE: Sort link list starting with head node
# Check if link list is empty or by itself
if (headNode == None) or (headNode.next == None):
return headNode
# Get middle node
middleNode = self.getMiddleNode(headNode)
nodeAfterMiddle = middleNode.next
# Set pointer from middleNode to next node as None
# NOTE: By setting next as none, middle_node would be the end of the link list
middleNode.next = None
# Sort left and right side of link list
leftSide = self.mergeSort(headNode)
rightSide = self.mergeSort(nodeAfterMiddle)
# Merge both sides of the link list to one in sorted order.
# The return value of sortedMerge() is the head node of the new link list
return self.sortedMerge(leftSide, rightSide)
def binarySearchForPosition(self, lowNode, highNode, position):
# OBJECTIVE: Return data at specific position
# Base cases
if lowNode == None:
# print("lowNode is none\n")
return
elif highNode == None:
# print("highNode is none\n")
return
if lowNode.pos > highNode.pos:
# print("Nodes are overlapping\n")
return
# Get 2 middle nodes
middleNode = self.getMiddleNode(lowNode)
nodeAfterMiddle = middleNode.next
# Temporarily cut link list in half
middleNode.next = None
# Make a recursive call, if needed
if middleNode.pos == position:
# print("Node found\n")
output = middleNode.data
elif middleNode.pos > position:
output = self.binarySearchForPosition(lowNode, middleNode.prev, position)
else:
output = self.binarySearchForPosition(nodeAfterMiddle, highNode, position)
# Relink link list and return output
middleNode.next = nodeAfterMiddle
return output
def binarySearchForNode(self, lowNode, highNode, position):
# OBJECTIVE: Return node at specified position
# Base cases
if lowNode == None:
# print("lowNode is none\n")
return
elif highNode == None:
# print("highNode is none\n")
return
if lowNode.pos > highNode.pos:
# print("Nodes are overlapping\n")
return
# Get 2 middle nodes
middleNode = self.getMiddleNode(lowNode)
nodeBeforeMiddle = middleNode.prev
nodeAfterMiddle = middleNode.next
# Temporarily cut link list in half
middleNode.next = None
# Make a recursive call, if needed
if middleNode.pos > position:
it = self.binarySearchForNode(lowNode, middleNode.prev, position)
elif middleNode.pos < position:
it = self.binarySearchForNode(nodeAfterMiddle, highNode, position)
else:
# Relink link list and set iterator node (it) as middleNode
middleNode.next = nodeAfterMiddle
middleNode.prev = nodeBeforeMiddle
it = middleNode
return it
# Incase in one recursive call the "else" was not met, then reconfigure link list
middleNode.next = nodeAfterMiddle
middleNode.prev = nodeBeforeMiddle
return it
def printForward(self):
# Get head node
currNode = self.getHead()
# Iterate link list
counter = 1
while currNode != None:
print("{}. {}".format(counter, currNode.data))
currNode = currNode.next
counter += 1
def printBackwards(self):
# Get head node
currNode = self.getTail()
# Iterate link list
counter = 1
while currNode != None:
print("{}. {}".format(counter, currNode.data))
currNode = currNode.prev
counter += 1
def printLinkListData(self):
# OBJECTIVE: Print data of each node inside link list
# Get head
currNode = self.getHead()
# Iterate link list
while currNode != None:
currNode.printInfo()
currNode = currNode.next