-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestLinkedList.py
373 lines (320 loc) · 10.1 KB
/
TestLinkedList.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# Description: Set of linkedlist functions including insert, delete, find, merge, sort
from typing import NewType
class Link (object):
def __init__(self, data, next = None):
self.data = data
self.next = None
def __str__ (self):
return str(self.data)
def __le__ (self, other):
return self.data <= other.data
class LinkedList (object):
# create a linked list
# you may add other attributes
def __init__ (self):
self.head = None
self.tail = None
self.numElements = 0
# get number of links
def get_num_links (self):
return self.numElements
# add an item at the beginning of the list
def insert_first (self, data):
new_Link = Link(data)
# newLink points to head
new_Link.next = self.head
# head is now newLink
self.head = new_Link
self.numElements += 1
# add an item at the end of a list
def insert_last (self, data):
new_link = Link(data)
self.numElements += 1
current = self.head
# if list is empty just add newLink as the head
if current == None:
self.head = new_link
return
while current.next != None:
current = current.next
current.next = new_link
# searches for specific link in data
def find_link(self, data):
current = self.head
# is list is empty return none
if current == None:
return None
# while current link is not the one we're searching for
while current.data != data:
if (current.next == None):
return None
else:
current = current.next
return current
# add an item in an ordered list in ascending order
# assume that the list is already sorted
def insert_in_order (self, data):
newLink = Link(data)
self.numElements += 1
# if head is null
if (self.head == None):
self.head = newLink
return
previous = self.head
current = self.head
# if data is already less than current data
if (data < current.data):
newLink.next = self.head
self.head = newLink
return
# while current Link <= data keep searching
while (current.data <= data):
if (current.next == None):
current.next = newLink
return
else:
previous = current
current = current.next
previous.next = newLink
newLink.next = current
# search in an unordered list, return None if not found
def find_unordered (self, data):
current = self.head
if current == None:
return None
# search through whole list till found
while current.data != data:
if (current.next == None):
return None
else:
current = current.next
return current
# Search in an ordered list, return None if not found
def find_ordered (self, data):
current = self.head
if current == None:
return None
# search through whole list while current data <= data
while current.data <= data:
if (current.data == data):
return current
if (current.next == None):
return None
else:
current = current.next
# Delete and return the first occurrence of a Link containing data
# from an unordered list or None if not found
def delete_link (self, data):
previous = self.head
current = self.head
if current == None:
return None
#while current data is not the data we're looking for, move current pointer forwards
while (current.data != data):
if (current.next == None):
return None
else:
previous = current
current = current.next
if (current == self.head):
self.head = self.head.next
else:
previous.next = current.next
self.numElements -= 1
return current
# String representation of data 10 items to a line, 2 spaces between data
def __str__ (self):
count = 1
current = self.head
string = ''
while (current != None):
# for every 10th link add a newline character
if count % 10 == 0 and count != 1:
string += str(current.data) + ' '
string += '\n'
else:
# add link and 2 spaces
string += str(current.data) + ' '
current = current.next
count += 1
return string
# Copy the contents of a list and return new list
# do not change the original list
def copy_list (self):
lst = LinkedList()
current = self.head
# loop through list and add links
for i in range(self.numElements):
newLink = current
lst.insert_last(newLink.data)
current = current.next
return lst
# Reverse the contents of a list and return new list
# do not change the original list
def reverse_list (self):
lst = LinkedList()
current = self.head
# loop through list and insert_first for each element
for i in range(self.numElements):
newLink = current
lst.insert_first(newLink.data)
current = current.next
return lst
# Sort the contents of a list in ascending order and return new list
# do not change the original list
def sort_list (self):
lst = LinkedList()
current = self.head
# using new list use insert_in_order to insert sorted
for i in range(self.numElements):
newLink = current
lst.insert_in_order(newLink.data)
#print(lst)
current = current.next
return lst
# Return True if a list is sorted in ascending order or False otherwise
def is_sorted (self):
# create new sorted version of list
lst = self.sort_list()
# check if sorted version = original version
return self.is_equal(lst)
# Return True if a list is empty or False otherwise
def is_empty (self):
return self.numElements == 0
# Merge two sorted lists and return new list in ascending order
# do not change the original lists
def merge_list (self, other):
lst = LinkedList()
current = self.head
current2 = other.head
length = min(self.numElements, other.numElements)
# loop through both Lists till the length of smallest list
while (current != None and current2 != None):
#for i in range(length):
if (current.data > current2.data):
lst.insert_last(current2.data)
current2 = current2.next
else:
lst.insert_last(current.data)
current = current.next
# add in remaining links
while (current != None):
lst.insert_last(current.data)
current = current.next
# add in remaining links
while (current2 != None):
lst.insert_last(current2.data)
current2 = current2.next
return lst
# Test if two lists are equal, item by item and return True
def is_equal (self, other):
if (self.numElements != other.numElements):
return False
current = self.head
current2 = other.head
# loop through list and check if each link = the other link
for i in range(self.numElements):
if (current.data != current2.data):
return False
else:
current = current.next
current2 = current2.next
return True
# Return a new list, keeping only the first occurence of an element
# and removing all duplicates. Do not change the order of the elements.
# do not change the original list
def remove_duplicates (self):
lst = self.copy_list()
memo = []
current = lst.head
previous = lst.head
# use memo to keep track of data we've seen
while (current != None):
# if element is not dup
if current.data not in memo:
memo.append(current.data)
previous = current
# if element is dup which means its in the memo, delete it
else:
previous.next = current.next
current = current.next
return lst
def main():
# Test methods insert_first() and __str__() by adding more than
# 10 items to a list and printing it.
lst = LinkedList()
for i in range(15,0,-1):
lst.insert_first(i)
#print(lst)
# Test method insert_last()
#lst.insert_last(16)
#print(lst)
# Test method insert_in_order()
#lst.insert_in_order(0)
#print(lst)
# Test method get_num_links()
# print(lst.get_num_links())
# Test method find_unordered()
# Consider two cases - data is there, data is not there
lst2 = LinkedList()
lst2.insert_first(1)
lst2.insert_first(27)
lst2.insert_first(13)
lst2.insert_first(17)
lst2.insert_first(-9)
#print(lst2.find_unordered(27))
#print(lst2.find_unordered(6))
# Test method find_ordered()
# Consider two cases - data is there, data is not there
#print(lst.find_ordered(5))
#print(lst.find_ordered(91))
# Test method delete_link()
# Consider two cases - data is there, data is not there
#print(lst.delete_link(6))
#print(lst.delete_link(91))
# Test method copy_list()
lst.copy_list()
#print(lst3)
# Test method reverse_list()
lst4 = lst.reverse_list()
#print(lst4)
# Test method sort_list()
lst5 = lst2.sort_list()
#print(lst5)
# Test method is_sorted()
# Consider two cases - list is sorted, list is not sorted
#print(lst5.is_sorted())
#print(lst2.is_sorted())
# Test method is_empty()
lst6 = LinkedList()
#print(lst2.is_empty())
#print(lst6.is_empty())
# Test method merge_list()
#print(lst5)
#print()
#lst7 = lst5.merge_list(lst5)
# print(lst7)
#print(lst)
#print()
#print(lst5)
#print()
lst8 = lst.merge_list(lst5)
#print(lst7)
#print(lst8)
# Test method is_equal()
# Consider two cases - lists are equal, lists are not equal
lst9 = lst8.copy_list()
#print(lst9.is_equal(lst8))
#print(lst.is_equal(lst9))
# Test remove_duplicates()
lst.insert_in_order(6)
lst.insert_first(15)
lst.insert_last(1)
lst.insert_last(7)
lst = lst.sort_list()
#print(lst)
#print()
lst = lst.remove_duplicates()
#print(lst)
if __name__ == "__main__":
main()