-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathList_ordered.py
101 lines (82 loc) · 2.38 KB
/
List_ordered.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
from List_node import Node
class OrderedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def size(self):
count = 0
current = self.head
while current != None:
count += 1
current = current.get_next()
return count
def __str__(self):
current = self.head
list1 = []
while current != None:
list1 = list1+[current.get_data()]
current = current.get_next()
return str(list1)
def remove(self, item):
current = self.head
prev = None
found = False
print self.head
while current != None and not found:
print current
print prev
if item == current.get_data():
found = True
else:
prev = current
current = current.get_next()
if prev == None:
self.head = current.get_next()
else:
prev.set_next(current.get_next())
def search(self, item):
found = False
current = self.head
stop = False
while current != None and not found and not stop:
if item < current.get_data():
stop = True
elif item == current.get_data():
found = True
else:
current = current.get_next()
return found
def add(self, item):
current = self.head
prev = None
stop = False
while current != None and not stop:
if item < current.get_data():
stop = True
else:
prev = current
current = current.get_next()
temp = Node(item)
if prev == None:
temp.set_next(self.head)
self.head = temp
else:
temp.next = prev.get_next()
prev.set_next(temp)
list1 = OrderedList()
print list1.is_empty()
list1.add(31)
list1.add(77)
list1.add(17)
list1.add(93)
list1.add(99)
print list1.size()
print(list1)
list1.remove(99)
#print list1.size()
#print list1.remove(54)
print list1.size()
print(list1)
print list1.search(77)
print list1.search(7)