-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
73 lines (57 loc) · 2.03 KB
/
Copy pathStack.py
File metadata and controls
73 lines (57 loc) · 2.03 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
class Elemento:
""" Creates an instance of Elemento. """
def __init__(self, valor):
""" Sets Element Value and Reference to next Element.
Reference needs to be None.
"""
self.valor = valor
self.siguiente = None
class Pila:
def __init__(self):
""" Creates a new Stack with size 0. """
self.primero = None
self.tamanio = 0
def agregar_elemento(self, valor):
""" Adds new element to the top of the Stack. """
Nuevo = Elemento(valor)
if self.tamanio == 0:
self.primero = Nuevo
else:
actual = self.primero
while actual.siguiente != None:
actual = actual.siguiente
actual.siguiente = Nuevo
self.tamanio += 1
def eliminar(self):
""" Deletes top element of the Stack. """
actual = self.primero
anterior = None
if self.tamanio != 0:
while actual.siguiente != None:
anterior = actual
actual = actual.siguiente
if anterior == None:
self.primero = None
else:
anterior.siguiente = None
self.tamanio -= 1
def ver_pila(self):
""" Shows the Stack
Stores values inside list just for aesthetics
"""
actual = self.primero
lista = []
while actual != None:
lista.append(actual.valor)
actual = actual.siguiente
return print(lista)
nueva_pila = Pila()
nueva_pila.agregar_elemento(1) #
nueva_pila.agregar_elemento(2) # Add 3 elements
nueva_pila.agregar_elemento(3) #
nueva_pila.ver_pila() # Show Stack
nueva_pila.eliminar() # Delete 1 element
nueva_pila.ver_pila() # Show Stack -1 element
nueva_pila.eliminar() # Deleting remaining elements
nueva_pila.eliminar() #
nueva_pila.ver_pila() # Show