-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistaEncadeada.py
51 lines (49 loc) · 1.55 KB
/
listaEncadeada.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
class No:
def __init__(self, item = None, proximo = None):
self.item = item
self.proximo = proximo
def __repr__(self):
return "_No({})".format(self.item.__repr__())
def __str__(self):
return self.item.__str__()
class Lista:
def __init__(self):
self.primeiro = self.ultimo = No()
def insert(self, valor):
self.ultimo.proximo = No(valor)
self.ultimo = self.ultimo.proximo
def delete(self, x):
if self.primeiro == self.ultimo:
raise ValueError("Não é possível remover itens de uma lista vazia")
anterior = self.primeiro
atual = anterior.proximo
while not atual is None and atual.item != x:
anterior = atual
atual = anterior.proximo
if atual == None:
raise ValueError("{} não pertence a lista".format(repr(x)))
else:
anterior.proximo = atual.proximo
atual.proximo = None
if self.ultimo == atual:
self.ultimo = anterior
del atual
## IMPLANTAR O LIST-SEARCH
def __str__(self):
string = ""
anterior = self.primeiro
atual = anterior.proximo
while not atual == None:
string += atual.item.__repr__()
if not atual is None:
string += ", "
anterior = atual
atual = anterior.proximo
return "[{}]".format(string)
lista = Lista()
lista.insert(6)
lista.insert(-39)
lista.insert(100)
print(lista.__str__())
lista.delete(6)
print(lista.__str__())