-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslList.py
More file actions
362 lines (279 loc) · 8.6 KB
/
slList.py
File metadata and controls
362 lines (279 loc) · 8.6 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
"""
File: slList.py
Purpose: rit_lib-based singly-linked list for CS141 LECTURE.
Author: ben k steele <bks@cs.rit.edu>
Author: sean strout <sps@cs.rit.edu>
Language: Python 3
Description: Implementation of a singly-linked list data structure.
***Modified :
By : Bri Miskovitz
How : integrated sorting function and its helper function using
the selection sort algorithm
"""
from rit_lib import *
from myNode import *
###########################################################
# LINKED LIST CLASS DEFINITION
###########################################################
class SlList(struct):
"""
SlList class encapsulates a node-based linked list.
'head' slot refers to a Node instance.
'size' slot holds the number of nodes in the list.
'cursor' slot refers to a Node instance.
"""
_slots = (((Node, NoneType), 'head') \
, (int, 'size') \
, ((Node, NoneType), 'cursor'))
###########################################################
# LINKED LIST CLASS CONSTRUCTOR
###########################################################
def createList():
"""
Create and return an instance
of an empty node-based, sin-linked list.
Parameters:
None
Returns:
An empty list
"""
return SlList(None, 0, None)
###########################################################
# CURSOR FUNCTIONS
###########################################################
def reset(lst):
"""
Resets the cursor to the start of the list
Parameters:
lst (SlList) - the linked list
Returns:
None
"""
lst.cursor = lst.head
def hasNext(lst):
"""
Returns True if the list has more elements.
Parameters:
lst (SlList) - the linked list
Returns:
True (bool) if the cursor refers to a valid Node
"""
return not lst.cursor == None
def next(lst):
"""
Returns the next element in the iteration.
Parameters:
lst (SlList) - the linked list
Preconditions:
If cursor is invalid, raises an IndexError exception
Returns:
The value (any type) referenced by the cursor
"""
if lst.cursor == None:
raise IndexError("cursor is invalid")
val = lst.cursor.data
lst.cursor = lst.cursor.next
return val
###########################################################
# LINKED LIST FUNCTIONS
###########################################################
def clear(lst):
"""
Make a list empty.
Parameters:
lst ( SlList ) - the linked list
Returns:
None
"""
lst.head = None
lst.size = 0
lst.cursor = None # invalidate cursor on clear()
def toString(lst):
"""
Converts the linked list into a string form that is similar to Python's
printed list.
Parameters:
lst (SlList) - The linked list
Returns:
A string representation of the list (e.g. '[1,2,3]')
"""
result = '['
curr = lst.head
while not curr == None:
if curr.next == None:
result += str(curr.data)
else:
result += str(curr.data) + ', '
curr = curr.next
result += ']'
return result
def append(lst, value):
"""
Add a node containing the value to the end of the list.
Parameters:
lst ( SlList ) - The linked list
value ( any type ) - The data to append to the end of the list
Returns:
None
"""
if lst.head == None:
lst.head = Node(value, None)
reset(lst)
else:
curr = lst.head
while curr.next != None:
curr = curr.next
curr.next = Node(value, None)
lst.size += 1
def insertAt(lst, idx, value):
"""
Insert a new element before the index.
Parameters:
lst ( SlList ) - The list to insert value into
idx ( int ) - The 0-based index to insert before
value ( any type ) - The data to be inserted into the list
Preconditions:
0 <= idx <= lst.size, raises IndexError exception
Returns:
None
"""
if idx < 0 or idx > lst.size:
raise IndexError(str(idx) + ' is out of range.')
if idx == 0:
lst.head = Node(value, lst.head)
else:
prev = lst.head
while idx > 1:
prev = prev.next
idx -= 1
prev.next = Node(value, prev.next)
lst.size += 1
def get(lst, idx):
"""
Returns the element that is at index in the list.
Parameters:
lst ( SlList ) - The list to insert value into
idx ( int ) - The 0-based index to get
Preconditions:
0 <= idx < lst.size, raises IndexError exception
Returns:
value at the index
"""
if idx < 0 or idx >= lst.size:
raise IndexError(str(idx) + ' is out of range.')
curr = lst.head
while idx > 0:
curr = curr.next
idx -= 1
return curr.data
def set(lst, idx, value):
"""
Sets the element that is at index in the list to the value.
Parameters:
lst ( SlList ) - The list to insert value into
idx ( int ) - The 0-based index to set
value ( any type )
Preconditions:
0 <= idx < lst.size, raises IndexError exception
Returns:
None
"""
if idx < 0 or idx >= lst.size:
raise IndexError(str(idx) + ' is out of range.')
curr = lst.head
while idx > 0:
curr = curr.next
idx -= 1
curr.data = value
def pop(lst, idx):
"""
pop removes and returns the element at index.
Parameters:
lst ( SlList ) - The list from which to remove
idx ( int ) - The 0-based index to remove
Preconditions:
0 <= idx < lst.size, raises IndexError exception
Returns:
The value ( any type ) being popped
"""
if idx < 0 or idx >= lst.size:
raise IndexError(str(idx) + ' is out of range.')
if idx == 0:
value = lst.head.data
lst.head = lst.head.next
else:
prev = lst.head
while idx > 1:
prev = prev.next
idx -= 1
value = prev.next.data
prev.next = prev.next.next
lst.size -= 1
lst.cursor = None # invalidate cursor on pop()
return value
def index(lst, value):
"""
Returns the index of the first occurrence of a value in the list
Parameters:
lst ( SlList ) - The list to insert value into
value ( any type ) - The data being searched for
Preconditions:
value exists in list, otherwise raises ValueError exception
Returns:
The 0-based index of value
"""
pos = 0
curr = lst.head
while not curr == None:
if curr.data == value:
return pos
pos += 1
curr = curr.next
raise ValueError(str(value) + " is not present in the list")
###########################################################
# SORT LIST FUNCTION
###########################################################
def linkSort(lst):
"""
linkSort sorts a linked list that consists of nodes
that contain an integer, and the list is sorted from
the node with the smallest integer to the node with the
largest integer
Parameters:
lst ( SlList ) - The list that will be sorted
Returns:
None
"""
if lst.size == 0:
pass
else:
currentNode = lst.head
while hasNext(lst) == True:
currIdx = index(lst, currentNode.data)
minIdx = currIdx + findMinFrom(currentNode)
temp = get(lst, minIdx) #temp = list[i]
set(lst, minIdx, currentNode.data)
set(lst, currIdx, temp) # list[i] = minVal
currentNode = currentNode.next
lst.cursor = lst.cursor.next
def findMinFrom(currentNode):
"""
findMinFrom
Parameters:
currentNode ( )
lst ( SlList ) - The list that will be sorted
Returns:
None
"""
i = 0
mi = 0
min = currentNode.data
while currentNode.next != None:
i += 1
newNode = currentNode.next
newVal = newNode.data
if newVal < min:
min = newVal
mi = i
currentNode=currentNode.next
return mi