-
Notifications
You must be signed in to change notification settings - Fork 1
/
basicDB-exercise2-unordered list.py
145 lines (119 loc) · 3.48 KB
/
basicDB-exercise2-unordered list.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
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
__author__ = 'Molly'
""" implement append, insert, index and pop methods for linked list"""
class UnorderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def search(self,item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
def remove(self,item):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
def __str__(self):
list_str = "head"
current = self.head
while current != None:
list_str = list_str + "->" + str(current.getData())
current = current.getNext()
list_str = list_str + "->" + str(None)
return list_str
def append(self, item):
"""add items into the linked from the other direction compared to add()"""
current = self.head
while current.getNext() != None:
current = current.getNext()
temp = Node(item)
temp.setNext(current.getNext())
current.setNext(temp)
def getIndex(self, item):
"""get the index of an item, assume the first one (head pointing to) is 0"""
index = 0
current = self.head
found = False
while current != None:
if current.getData() == item:
found = True
break
else:
current = current.getNext()
index += 1
if not found:
index = None
return index
def getItem(self, index):
"""return an item given an index"""
current = self.head
for i in range(index):
current = current.getNext()
if current != None:
return current.getData()
else:
raise("index out of range")
def pop(self, index):
self.remove(self.getItem(index))
def insert(self, index, item):
"""insert an item after index item"""
current = self.head
for i in range(index):
current = current.getNext()
if current != None:
temp = Node(item)
temp.setNext(current.getNext())
current.setNext(temp)
else:
raise("index out of range")
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
alist = UnorderedList()
alist.add(30)
alist.add(31)
alist.add(27)
alist.append(100)
alist.append(101)
print alist
print alist.getIndex(27)
print alist.getItem(4)
alist.pop(4)
print alist
alist.insert(1, 5)
print alist