-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnorderedList.py
271 lines (195 loc) · 6.52 KB
/
UnorderedList.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
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
from Node import Node
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 += 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 current != None and not found :
if current.getData () == item :
found = True
else :
previous = current
current = current.getNext ()
if previous == None :
self.head = current.getNext ()
elif current == None :
print ("{} is not exist in the list.".format (item))
else :
previous.setNext (current.getNext ())
def append (self , item) :
temp = Node (item)
temp.setNext (None)
if self.head == None :
self.head = temp
else :
current = self.head
previous = None
found = False
while not found :
if current == None :
found = True
else :
previous = current
current = current.getNext ()
previous.setNext (temp)
def index (self , item) :
current = self.head
found = False
count = 0
while current != None and not found :
if current.getData () == item :
found = True
else :
count += 1
current = current.getNext ()
if not found :
return "{} is not exist in the list.".format (item)
else :
return count
def pop (self , item = None) :
current = self.head
previous = None
data = None
found = False
count = 0
if item == None :
while current.getNext () != None :
previous = current
current = current.getNext ()
data = current.getData()
if previous == None :
self.head = current.getNext ()
else :
previous.setNext (current.getNext ())
else :
if item < 0 :
while current != None :
count -= 1
current = current.getNext ()
current = self.head
while current != None and not found :
if count == item :
found = True
else :
count += 1
previous = current
current = current.getNext ()
if found :
data = current.getData()
if previous == None :
self.head = current.getNext ()
else :
previous.setNext (current.getNext ())
else :
data = "The list doesn't have {}. index.".format (item)
return data
def insert (self , index , item) :
temp = Node (item)
current = self.head
previous = None
found = False
count = 0
if current == None :
self.head = temp
else :
if index < 0 :
while current != None :
count -= 1
current = current.getNext ()
current = self.head
while current != None and not found :
if count == index :
found = True
else :
count += 1
previous = current
current = current.getNext ()
if found :
if previous == None :
temp.setNext (current)
self.head = temp
else :
temp.setNext (current)
previous.setNext (temp)
else :
print ("The list doesn't have {}. index.".format (index))
def slice (self , begin = 0 , end = None , leap = 1) :
try :
if begin < 0 or leap < 0 :
raise TypeError
else :
if end != None and end < 0 :
raise TypeError
except TypeError :
return "The method can execute just with unsigned integers."
current = self.head
count = 0
size = 0
item_list = '['
while current != None :
size += 1
current = current.getNext ()
current = self.head
if end == None :
end = size
elif end > size :
return "The list doesn't have {}. index.".format (end)
if begin > size :
return "The list doesn't have {}. index".format (begin)
while count < end :
if count == begin :
if (count + 1) == end :
item_list += str (current.getData ()) + ']'
else :
item_list += str (current.getData ()) + ', '
begin += leap
count += 1
current = current.getNext ()
return item_list
def __len__ (self) : # this len method is count the number of items in the list
current = self.head # and then adds the count value to the head of the list as a node
count = 0
while current != None :
count += 1
current = current.getNext ()
temp = Node (count)
temp.setNext (self.head)
self.head = temp
return self.head.getData ()
def __str__ (self) :
if self.head == None :
return "[]"
else :
current = self.head
item_list = '['
while current != None :
if current.getNext () != None :
item_list += str (current.getData ()) + ", "
else :
item_list += str (current.getData ()) + ']'
current = current.getNext ()
return item_list