-
Notifications
You must be signed in to change notification settings - Fork 0
/
pila.py
40 lines (27 loc) · 834 Bytes
/
pila.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
from lista_enlazada import ListaEnlazada
class _IteradorPila:
def __init__(self, prim):
self.actual = prim
def __next__(self):
if self.actual is None:
raise StopIteration()
dato = self.actual.dato
self.actual = self.actual.prox
return dato
class Pila:
def __init__(self):
self.items = ListaEnlazada()
def __str__(self):
return str(self.items)
def __iter__(self):
return _IteradorPila(self.items.prim)
def esta_vacia(self):
return self.items.prim is None
def apilar(self, x):
self.items.insert(x, 0)
def desapilar(self):
if self.esta_vacia():
raise ValueError('La pila está vacía.')
return self.items.pop(0)
def ver_tope(self):
return self.items.prim.dato