-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderedlist function implementation program.py
More file actions
298 lines (213 loc) · 6.6 KB
/
orderedlist function implementation program.py
File metadata and controls
298 lines (213 loc) · 6.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
# Implementation of an Unordered List ADT as a linked list. The list
# is accessed through a reference to the first element, head.
# Adopted from the textbook.
class Node:
'''
Create a Node object and initialize its data.
'''
def __init__(self, init_data):
self.data = init_data
self.next = None
'''
Accessor for node data
'''
def get_data(self):
return self.data
'''
Accessor for next reference
'''
def get_next(self):
return self.next
'''
Mutator for node data
'''
def set_data(self, new_data):
self.data = new_data
'''
Mutator for next reference
'''
def set_next(self, new_next):
self.next = new_next
class OrderedList:
'''
List is empty upon creation and the head reference is None
'''
def __init__(self):
self.head = None
'''
Returns True if list is empty, False otherwise
'''
def is_empty(self):
return self.head == None
'''
Add an element to head of the list
'''
def add(self, item):
# keep track of current and previous elements
current = self.head
previous = None
stop = False
while current != None and not stop:
# if we have a match, stop
if current.get_data() > item:
stop = True
# otherwise advance current and next references
else:
previous = current
current = current.get_next()
# Create a node using item as its data
temp = Node(item)
if previous == None:
temp.set_next(current)
self.head = temp
# the element to be deleted is not the head
else:
temp.set_next(current)
previous.set_next(temp)
'''
Returns the size of the list
'''
def size(self):
# start at the head of the list
current = self.head
count = 0
# Traverse the list one element at a time. We know
# we reached the end when the next reference is None
while current != None:
count = count + 1
current = current.get_next()
return count
'''
Search for an item in the list. Returns True if found, False otherise.
'''
def search(self,item):
current = self.head
found = False
stop=False
# As long as the element is not found and we haven't
# reached the end of the list
while current != None and not found and not stop:
if current.get_data() == item:
found = True
else:
if current.get_data()>item:
stop= True
else:
# go to the next element
current = current.get_next()
return found
'''
Remove the first occurrence of item from the list.
'''
def remove(self, item):
# keep track of current and previous elements
current = self.head
previous = None
found = False
# traverse the list
while current != None and not found:
# if we have a match, stop
if current.get_data() == item:
found = True
# otherwise advance current and next references
else:
previous = current
current = current.get_next()
# the element to be deleted is the head of the list
if found:
if previous == None:
self.head = current.get_next()
# the element to be deleted is not the head
else:
previous.set_next(current.get_next())
def print_list(self):
current = self.head
while(current):
if(current.next):
print(current.data,end=",")
else:
print(current.data)
current = current.next
def index(self,item):
index=0
current=self.head
while current != None:
if current.get_data()== item:
return index
else:
index= index+1
current=current.get_next()
def pop(self):
previous = None
current = False
current=self.head
while current.get_next() != None:
previous = current
current=current.get_next()
previous.set_next(None)
return current
def count(self):
count=0
current=self.head
while current != None:
current=current.get_next()
count+=1
return count
def duplicate(self):
items=[]
unique_items=[]
current=self.head
while current != None:
items.append(current.get_data())
if current.get_data() not in unique_items:
unique_items.append(current.get_data())
current=current.get_next()
current=self.head
for item in unique_items:
current.set_data(item)
if item!= unique_items[-1]:
current=current.get_next()
current.set_next(None)
return items
def pop_pos(self,item):
count=0
previous=None
current=self.head
while current!= None and count != item:
count +=1
previous=current
current=current.get_next()
else:
previous.set_next(current.get_next())
return current
def main():
# create a list and add some elements to it
aList = OrderedList()
print("Adding 15 random intergers ranging 1-5 to the list.")
aList.add(1)
aList.add(2)
aList.add(3)
aList.add(5)
aList.add(1)
aList.add(2)
aList.add(3)
aList.add(5)
aList.add(1)
aList.add(2)
aList.add(3)
aList.add(5)
aList.add(1)
aList.add(2)
aList.add(3)
aList.add(5)
aList.print_list()
print("the count for the list is:",aList.count())
print("the popped number is:",aList.pop().get_data())
print("the count for the list is:",aList.count())
print("the popped pos is:",aList.pop_pos(2).get_data())
aList.print_list()
print("the index of 8 is:",aList.index(5))
print("the duplicates of the list:",aList.duplicate())
aList.print_list()
if __name__ == "__main__":
main()