-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.py
executable file
·299 lines (237 loc) · 8.55 KB
/
linked_list.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
################################################################################
from common import *
################################################################################
class CLinkedListNode:
#---------------------------------------------------------------------------
def __init__(self, val = None, next = None, prev = None):
self.val = val
self.next = next
self.prev = prev
#---------------------------------------------------------------------------
def IsLast(self):
if self.next == None:
return 1
else:
return 0
#---------------------------------------------------------------------------
def IsFirst(self):
if self.prev == None:
return 1
else:
return 0
#---------------------------------------------------------------------------
def GetNext(self):
return self.next
#---------------------------------------------------------------------------
def GetPrev(self):
return self.prev
#---------------------------------------------------------------------------
def GetVal(self):
return self.val
################################################################################
class CLinkedListIterator:
#---------------------------------------------------------------------------
def __init__(self, n):
self.idx = 0
self.base = n
self.n = None
#---------------------------------------------------------------------------
def Next(self):
if self.base == None:
# No base node
return 0
if self.n == None:
# Select first node
self.idx = 0
self.n = self.base
return 1
if self.n.IsLast():
# Last node on the list
return 0
# Select next node
self.idx += 1
self.n = self.n.GetNext()
return 1
#---------------------------------------------------------------------------
def Prev(self):
if self.base == None:
# No base node
return 0
if self.n == None:
# Select first node
self.idx = 0
self.n = self.base
return 1
if self.n.IsFirst():
# First node on the list
return 0
# Select prev node
self.idx -= 1
self.n = self.n.GetPrev()
return 1
#---------------------------------------------------------------------------
def GetIdx(self):
return self.idx
#---------------------------------------------------------------------------
def GetVal(self):
ASSERT(self.n != None, "Invalid node")
return self.n.val
################################################################################
class CLinkedList:
#---------------------------------------------------------------------------
def __init__(self):
self.first = None
self.last = None
self.size = 0
#---------------------------------------------------------------------------
def GetFirstNode(self):
return self.first
#---------------------------------------------------------------------------
def GetSize(self):
return self.size
#---------------------------------------------------------------------------
def GetNodeByIdx(self, idx):
ASSERT(idx >= 0 and idx < self.size, "Invalid index")
i = 0
n = self.first
while i != idx:
n = n.next
i += 1
return n
#---------------------------------------------------------------------------
def GetNodeByVal(self, val):
n = self.first
while n.val != val:
n = n.next
return n
#---------------------------------------------------------------------------
def GetValByIdx(self, idx):
return self.GetNodeByIdx(idx).val
#---------------------------------------------------------------------------
def AppendVal(self, val):
n = CLinkedListNode(val)
self.Append(n)
return n
#---------------------------------------------------------------------------
def Append(self, n):
n.next = None
n.prev = self.last
if self.size == 0:
# List is empty
# Validate first/last pointers
ASSERT(self.first == None, "Invalid first pointer")
ASSERT(self.last == None, "Invalid last pointer")
# Append first node to list
self.first = n
self.last = n
else:
# List is not empty
# Validate first/last pointers
ASSERT(self.first != None, "Invalid first pointer")
ASSERT(self.last != None, "Invalid last pointer")
# Append node to the end of the list
self.last.next = n
self.last = n
# Increment node counter
self.size += 1
ASSERT(self.size > 0, "Invalid node counter")
#---------------------------------------------------------------------------
def RemoveVal(self, val):
n = self.GetNodeByVal(val)
ASSERT(n != None, "Value not found")
self.Remove(n)
#---------------------------------------------------------------------------
def Remove(self, n):
if self.size == 1:
# Remove last element from list
# Node should be first/last in the list
ASSERT(self.first == self.last == n, "Invalid node pointer")
# Remove first/last pointers
self.first = None
self.last = None
else:
# Remove node from the middle of the list
# List should not be empty
ASSERT(self.size != 0, "List is empty")
if self.first == n:
# Remove first node from the list
# Second node becomes first
self.first = n.next
n.next.prev = None
elif self.last == n:
# Remove node from the end of the list
# Pre-last node becomes last
self.last = n.prev
n.prev.next = None
else:
# Remove node somewhere from the middle of the list
n.prev.next = n.next
n.next.prev = n.prev
# Clear next/prev node pointers
n.next = None
n.prev = None
# Decrement node counter
self.size -= 1
ASSERT(self.size >= 0, "Invalid node counter")
#---------------------------------------------------------------------------
def Clear(self):
if self.size == 0:
# List should be empty
# Validate first/last pointers
ASSERT(self.first == None, "Invalid first pointer")
ASSERT(self.last == None, "Invalid last pointer")
else:
# List should not be empty
# Validate first/last pointers
ASSERT(self.first != None, "Invalid first pointer")
ASSERT(self.last != None, "Invalid last pointer")
# Iterate list and remove links from each node
n = self.first
while n != None:
next = n.next
n.next = None
n.prev = None
n = next
self.size -= 1
# Clear first/last pointer
self.first = None
self.last = None
# Size of list shoul be 0
ASSERT(self.size == 0, "Invalid list size")
#---------------------------------------------------------------------------
def Iterate(self, f):
i = 0
n = self.first
while n != None:
if f(i, n) == 0:
break
n = n.next
i += 1
#---------------------------------------------------------------------------
def GetIterator(self):
return CLinkedListIterator(self.first)
################################################################################
def Test():
l = CLinkedList()
n0 = l.AppendVal("eat")
n1 = l.AppendVal("my")
n2 = l.AppendVal("shorts")
ASSERT(l.size == 3, "Invalid size")
n = l.first
while n != None:
print n.val
n = n.next
def DumpList(i, n):
print "%i - %s" % (i, n.val),
if n.IsFirst():
print "<First>",
if n.IsLast():
print "<Last>",
print ""
l.Iterate(DumpList)
l.Remove(n1)
l.Iterate(DumpList)
ASSERT(l.size == 2, "Invalid size")
l.Clear()
l.Iterate(DumpList)
ASSERT(l.size == 0, "Invalid size")