-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLList.py
256 lines (200 loc) · 6.95 KB
/
DLList.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
class dllNode:
def __init__(self, data, next=None, prev=None):
self.__data = data
self.__next = next
self.__prev = prev
def getData(self):
return self.__data
def getNext(self):
return self.__next
def setNext(self, next):
self.__next = next
def getPrev(self):
return self.__prev
def setPrev(self, prev):
self.__prev = prev
class DLinkedList:
def __init__(self):
self.__root = None
self.__nElms = 0
# para o GetFirst e o GetNext
self.__mov = None
def __str__(self):
if self.__root is None:
return "EMPTY"
if self.__nElms == 1:
outstr = "NULL <-- "
outstr += str(self.__root.getData().getVal())
outstr += " --> NULL"
return outstr
outstr = "<-- "
aux = self.__root
while aux is not None and aux.getNext() is not self.__root:
outstr += str(aux.getData().getVal()) + " <--> "
aux = aux.getNext()
outstr += str(aux.getData().getVal()) + " --> "
return outstr
def getNelms(self):
return self.__nElms
def insertEnd(self, data):
if self.__root is None:
self.__root = dllNode(data)
else:
newNode = dllNode(data)
if self.__nElms == 1:
self.__root.setPrev(newNode)
self.__root.setNext(newNode)
newNode.setPrev(self.__root)
newNode.setNext(self.__root)
else:
last = self.__root.getPrev()
self.__root.setPrev(newNode)
last.setNext(newNode)
newNode.setPrev(last)
newNode.setNext(self.__root)
self.__nElms += 1
def insertStart(self, data):
if self.__root is None:
self.__root = dllNode(data)
else:
newNode = dllNode(data)
if self.__nElms == 1:
self.__root.setPrev(newNode)
self.__root.setNext(newNode)
newNode.setPrev(self.__root)
newNode.setNext(self.__root)
else:
newNode.setNext(self.__root)
newNode.setPrev(self.__root.getPrev())
last = self.__root.getPrev()
self.__root.setPrev(newNode)
last.setNext(newNode)
self.__root = newNode
self.__nElms += 1
def removeEnd(self):
if self.__root is None:
return None
if self.__nElms == 1:
node = self.__root
self.__root = None
elif self.__nElms == 2:
node = self.__root.getNext()
self.__root.setNext(None)
self.__root.setPrev(None)
else:
node = self.__root.getPrev()
last = node.getPrev()
last.setNext(self.__root)
self.__root.setPrev(last)
self.__nElms -= 1
return node.getData()
def removeStart(self):
if self.__root is None:
return None
if self.__nElms == 1:
node = self.__root
self.__root = None
elif self.__nElms == 2:
node = self.__root
self.__root = self.__root.getNext()
self.__root.setNext(None)
self.__root.setPrev(None)
else:
node = self.__root
self.__root = self.__root.getNext()
self.__root.setPrev(node.getPrev())
node.getPrev().setNext(self.__root)
self.__nElms -= 1
return node.getData()
def insertOrd(self, data):
if self.__root is None:
self.__root = dllNode(data)
self.__nElms += 1
else:
if self.__root.getData().getVal() > data.getVal():
self.insertStart(data)
elif self.__nElms == 1:
self.insertEnd(data)
else:
aux = self.__root
while aux.getNext().getData().getVal() <= data.getVal():
aux = aux.getNext()
if aux == self.__root:
break
if aux == self.__root:
self.insertEnd(data)
else:
newNode = dllNode(data)
nextAux = aux.getNext()
newNode.setPrev(aux)
newNode.setNext(nextAux)
aux.setNext(newNode)
nextAux.setPrev(newNode)
self.__nElms += 1
def searchDelete(self, key):
if self.__root is None:
return None
aux = self.__root
while aux.getData().getVal() != key:
aux = aux.getNext()
if aux == self.__root:
break
if aux.getData().getVal() == key:
if aux == self.__root:
return self.removeStart()
if aux == self.__root.getPrev():
return self.removeEnd()
prev = aux.getPrev()
next = aux.getNext()
prev.setNext(next)
next.setPrev(prev)
self.__nElms -= 1
return aux.getData()
return None
def search(self, key):
if self.__root is None:
return None
node = self.__root
while node is not None:
if node.getData().getVal() == key:
return node.getData()
node = node.getNext()
if node == self.__root:
break
return None
def lenght(self):
return self.__nElms
def getFirst(self):
if self.__root is None:
return None
self.__mov = self.__root
return self.__mov.getData()
def getNext(self):
if self.__mov is None:
return None
self.__mov = self.__mov.getNext()
if self.__mov == self.__root:
self.__mov = None
return None
if self.__mov is None:
return None
return self.__mov.getData()
def getLast(self):
if self.__root is None:
return None
if self.__nElms == 1:
self.__mov = self.__root
else:
self.__mov = self.__root.getPrev()
return self.__mov.getData()
def getPrev(self):
if self.__mov is None:
return None
# Verificar mesmo se nao tem bugs aqui
self.__mov = self.__mov.getPrev()
if self.__mov == self.__root.getPrev():
self.__mov = None
return None
if self.__mov is None:
return None
return self.__mov.getData()