-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderedList.py
91 lines (79 loc) · 2.58 KB
/
OrderedList.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
import Node
'''
Implement ordered list via Node per https://interactivepython.org/runestone/static/pythonds/BasicDS/ImplementinganOrderedList.html
'''
class OrderedList:
def __init__(self):
self.head = None
self.tail = None
def isEmpty(self):
if self.head == None:
return True
else:
return False
def search(self, item):
found = False
currentNode = self.head
while currentNode != None:
# If item is found, set found to True and break
if currentNode.getData() == item:
found = True
break
# If node value is greater than item, stop iteration because the item is not in the list
elif currentNode.getData() > item:
break
else:
currentNode = currentNode.getNextNode()
return found
# add the item in ascending order
def add(self, item):
newNode = Node.Node(item)
previousNode = None
if self.head == None:
self.head = newNode
else:
currentNode = self.head
while currentNode != None:
if currentNode.getData() <= item:
if currentNode.getNextNode() == None:
currentNode.setNextNode(newNode)
break
else:
previousNode = currentNode
currentNode = currentNode.getNextNode()
elif currentNode.getData() > item:
if previousNode == None:
newNode.setNextNode(currentNode)
self.head = newNode
else:
newNode.setNextNode(currentNode)
previousNode.setNextNode(newNode)
break
def size(self):
count = 0
current = self.head
while current != None:
count += 1
current = current.getNextNode()
return count
def index(self):
return None
def remove(self, item):
return None
def pop(self):
currentNode = self.head
self.head = currentNode.getNextNode()
return currentNode.getData()
if __name__ =="__main__":
mylist = OrderedList()
mylist.add(31)
mylist.add(77)
mylist.add(17)
mylist.add(93)
mylist.add(26)
mylist.add(54)
print(mylist.size())
print(mylist.search(93))
print(mylist.search(100))
for i in range(mylist.size()):
print(mylist.pop())