-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.py
More file actions
94 lines (72 loc) · 2.44 KB
/
Copy pathLinkedList.py
File metadata and controls
94 lines (72 loc) · 2.44 KB
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
class Nodo:
"""
Node class.
Attributes:
valor = Node value.
ref = Reference to next Node.
"""
def __init__(self, Valor):
self.valor = Valor
self.ref = None
class ListaEnlazada:
def __init__(self):
""" Creates a new Linked List.
Starts with size = 0 and has no First Node.
"""
self.primero = None
self.tamanio = 0
def agregar_nodo(self, Valor):
""" Adds new Node at end of List. """
Nuevo = Nodo(Valor)
if self.tamanio == 0 :
self.primero = Nuevo
else:
actual = self.primero
while actual.ref != None:
actual = actual.ref
actual.ref = Nuevo
self.tamanio = self.tamanio + 1
def agregar_inicio(self, valor):
""" Adds new Node at the beginning of the List. """
Nuevo = Nodo(valor)
if self.tamanio == 0:
self.primero = Nuevo
else:
temp = self.primero
self.primero = Nuevo
self.primero.ref = temp
self.tamanio += 1
def quitar_nodo(self, Valor):
""" Removes a Node matching the value. """
actual = self.primero
if self.tamanio != 0:
while actual.valor != Valor:
if actual.ref == None:
break
else:
anterior = actual
actual = actual.ref
if actual != None:
if actual == self.primero:
self.primero = actual.ref
else:
anterior.ref = actual.ref
self.tamanio -= 1
def verlista(self):
""" Shows the List. """
actual = self.primero
lista = []
while actual!= None:
lista.append(actual.valor)
actual = actual.ref
return print(lista)
NuevaLista = ListaEnlazada()
NuevaLista.agregar_nodo(1) #
NuevaLista.agregar_nodo(2) #
NuevaLista.agregar_nodo(3) # Adds 3 Nodes.
NuevaLista.agregar_inicio(4) # Adds Node at Start.
NuevaLista.agregar_inicio(7) # Adds Node at Start.
NuevaLista.verlista() # Shows List.
NuevaLista.quitar_nodo(7) # Removes Node.
NuevaLista.quitar_nodo(1) # Removes Node.
NuevaLista.verlista() # Shows.