Repository navigation
Expand file tree
/
Copy pathTestLinkedList.py
More file actions
384 lines (337 loc) · 11.5 KB
/
Copy pathTestLinkedList.py
File metadata and controls
384 lines (337 loc) · 11.5 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
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
374
375
376
377
378
379
380
381
382
383
384
# File: TestLinkedList.py
# Description: Creates a Link and LinkedList class,
# creates and tests multiple helper methods to expand
# functionality
# Student Name: Mark Simmons
# Student UT EID: mjs5839
# Partner Name: Daniel Espinoza
# Partner UT EID: dee467
# Course Name: CS 313E
# Unique Number: 51350
# Date Created: 11/4/18
# Date Last Modified: 11/16/18
class Link(object):
def __init__(self, data, next=None):
self.data = data
self.next = next
def __str__(self):
return(str(self.data))
class LinkedList(object):
def __init__(self):
self.first = None
# insert link at beginning of list
def insert_first(self, data):
new_link = Link(data)
new_link.next = self.first
self.first = new_link
# insert link at end of list
def insert_last(self, data):
new_link = Link(data)
current = self.first
# set new_link as first if list empty
if (current == None):
self.first = new_link
return
# traverse list to end
while (current.next != None):
current = current.next
current.next = new_link
# searches list for given link,
def find_link(self, data):
current = self.first
if (current == None):
return None
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
def insert_in_order (self, data):
current = self.first
if current.data >= data:
self.insert_first(data)
return
while (current != None):
if current.next.data != None and current.next.data >= data:
current.next = Link(data, current.next)
return
current = current.next
# return length of linked list
def get_num_links(self):
length=0
temp=self.first
while (temp != None):
length+=1
temp = temp.next
return length
# search in an unordered list, return None if not found
def find_unordered (self, data):
temp=self.first
if temp.data == data:
return True
while (temp.next != None):
if temp.next.data == data:
return True
temp = temp.next
return None
# Search in an ordered list, return None if not found
def find_ordered (self, data):
current = self.first
while (current != None):
if current.data == data:
return True
elif current.data > data:
return None
current = current.next
return None
# Delete and return Link from an unordered list or None if not found
def delete_link(self, data):
previous = self.first
current = self.first
if (current == None):
return None
while (current.data != data):
if (current.next == None):
return None
else:
previous = current
current = current.next
if (current == self.first):
self.first = self.first.next
else:
previous.next = current.next
return current.data
# String representation of data 10 items to a line, 2 spaces between data
def __str__(self):
strg = ""
temp = self.first
if self.first==None:
return strg
while (temp != None):
strg += str(temp.data)
temp = temp.next
strg += " "
if self.first.next == None:
strg += str(self.first.data)
return strg
# Copy the contents of a list and return new list
def copy_list (self):
temp=self.first
copy=LinkedList()
while (temp != None):
copy.insert_last(temp.data)
temp = temp.next
return copy
# Reverse the contents of a list and return new list
def reverse_list (self):
temp = self.first
reverse = LinkedList()
while (temp != None):
reverse.insert_first(temp.data)
temp = temp.next
return reverse
# Sort the contents of a list in ascending order and return new list
def sort_list(self):
temp=self.first
temp2=self.copy_list()
sorted=LinkedList()
while(sorted.get_num_links()!=self.get_num_links()):
min=temp.data
while (temp !=None):
if temp.data<min:
min=temp.data
temp=temp.next
sorted.insert_last(min)
temp2.delete_link(min)
temp=temp2.first
return sorted
# Return True if a list is sorted in ascending order or False otherwise
def is_sorted (self):
current = self.first
previous = current
while (current != None):
if current.data < previous.data:
return False
previous = current
current = previous.next
return False
# Return True if a list is empty or False otherwise
def is_empty (self):
return self.first==None
# Merge two sorted lists and return new list in ascending order
def merge_list (self, other):
s_current = self.first
o_current = other.first
return_list = LinkedList()
while (s_current != None) and (o_current != None):
if (s_current.data < o_current.data):
return_list.insert_last(s_current)
s_current = s_current.next
else:
return_list.insert_last(o_current)
o_current = o_current.next
if (s_current == None):
while (o_current != None):
return_list.insert_last(o_current)
o_current = o_current.next
elif (o_current == None):
while (s_current != None):
return_list.insert_last(s_current)
s_current = s_current.next
return return_list
# Test if two lists are equal, item by item and return True
def is_equal (self, other):
s_current = self.first
o_current = other.first
while (s_current != None) or (o_current != None):
if s_current == None:
return False
elif o_current == None:
return False
elif s_current.data != o_current.data:
return False
else:
s_current = s_current.next
o_current = o_current.next
# Return a new list, keeping only the first occurence of an element
# and removing all duplicates. Do not change the order of the elements.
def remove_duplicates (self):
links = {}
return_list = self.copy_list()
return_list=return_list.reverse_list()
current = self.first
while (current != None):
links[current.data] = True
current = current.next
current = self.first
while (current != None):
if links[current.data]:
links[current.data] = False
elif not links[current.data]:
links[current.data]="dup"
# delete this entry if encountered again
if links[current.data]=="dup":
return_list.delete_link(current.data)
current = current.next
return_list=return_list.reverse_list()
return return_list
def main():
list_1 = LinkedList()
# Test methods insert_first() and __str__() by adding more than
# 10 items to a list and printing it.
print("Testing insert_first and __str__")
list_1.insert_first(8)
list_1.insert_first(-5)
list_1.insert_first(-2)
list_1.insert_first(-6)
list_1.insert_first(4)
list_1.insert_first(42)
list_1.insert_first(3)
list_1.insert_first(0)
list_1.insert_first(9)
list_1.insert_first(2)
list_1.insert_first(10)
print(list_1)
# Test method insert_last()
print()
print("Testing insert_last")
list_1.insert_last(44)
print(list_1)
# Test method insert_in_order()
print()
print("Testing insert_in_order")
list_2 = LinkedList()
list_2.insert_last(1)
list_2.insert_last(2)
list_2.insert_last(3)
list_2.insert_last(5)
list_2.insert_last(6)
print("List before inserting 4:", list_2)
list_2.insert_in_order(4)
print("List after insertion", list_2)
# Test method get_num_links()
print()
print("Testing get_num_links")
print("list_1 should return 12 and list_2 6")
print("list_1: {a}, list_2: {b}".format(a = list_1.get_num_links(), b = list_2.get_num_links()))
# Test method find_unordered()
# Consider two cases - data is there, data is not there
print()
print("Testing find_unordered")
print("First case should return True, second None")
print(list_1.find_unordered(10))
print(list_1.find_unordered(55))
# Test method find_ordered()
# Consider two cases - data is there, data is not there
print()
print("Testing find_ordered")
print("First case should return True, second None")
print(list_2.find_ordered(5))
print(list_2.find_ordered(7))
# Test method delete_link()
# Consider two cases - data is there, data is not there
print()
print("Testing delete_link")
print("First case should return 2, second None")
print(list_1.delete_link(2))
print(list_1.delete_link(55))
# Test method copy_list()
print()
print("Testing copy_list")
print("Both lists should be equivalent")
copy=list_1.copy_list()
print(list_1)
print(copy)
# Test method reverse_list()
print()
print("Testing reverse_list")
print(list_1)
reverse=list_1.reverse_list()
print(reverse)
# Test method sort_list()
print()
print("Testing sort_list")
print("Second list should be sorted")
r=list_1.sort_list()
print(list_1)
print(r)
# Test method is_sorted()
# Consider two cases - list is sorted, list is not sorted
print()
print("Testing is_sorted")
print("First case should return True, second False")
print(list_1, list_1.is_sorted())
print(list_2, list_2.is_sorted())
# Test method is_empty()
print()
print("Testing is_empty")
print("First case should return True, second False")
t=LinkedList()
print(t, t.is_empty())
print(list_2, list_2.is_empty())
# Test method merge_list()
print()
print("Testing merge_list")
print(r)
print(list_2)
print("Merged:", r.merge_list(list_2))
# Test method is_equal()
# Consider two cases - lists are equal, lists are not equal
print()
print("Testing is_equal")
print("First case should return True, second False")
print("list_1, list_1.copy_list():", list_1.is_equal(copy))
print("list_1, list_2", list_1.is_equal(list_2))
# Test remove_duplicates()
print()
print("Testing remove_duplicates")
list_1.insert_last(8)
list_1.insert_last(4)
list_1.insert_last(9)
list_1.insert_last(2)
print(list_1)
print("Without duplicates:", list_1.remove_duplicates())
if __name__ == "__main__":
main()