-
-
Save parzibyte/e8adcbd6ae1602da2a678acde486f20e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
https://parzibyte.me/blog | |
""" | |
class Nodo: | |
def __init__(self, dato): | |
self.dato = dato | |
self.siguiente = None | |
class Pila: | |
def __init__(self): | |
self.superior = None | |
def apilar(self, dato): | |
print(f"Agregando {dato} en la cima de la pila") | |
# Si no hay datos, agregamos el valor en el elemento superior y regresamos | |
if self.superior == None: | |
self.superior = Nodo(dato) | |
return | |
nuevo_nodo = Nodo(dato) | |
nuevo_nodo.siguiente = self.superior | |
self.superior = nuevo_nodo | |
def desapilar(self): | |
# Si no hay datos en el nodo superior, regresamos | |
if self.superior == None: | |
print("No hay ningún elemento en la pila para desapilar") | |
return | |
print(f"Desapilar {self.superior.dato}") | |
self.superior = self.superior.siguiente | |
def imprimir(self): | |
print("Imprimiendo pila:") | |
# Recorrer la pila e imprimir valores | |
nodo_temporal = self.superior | |
while nodo_temporal != None: | |
print(f"{nodo_temporal.dato}", end=",") | |
nodo_temporal = nodo_temporal.siguiente | |
print("") | |
pila = Pila() | |
pila.apilar("Luis") | |
pila.apilar("María José") | |
pila.apilar("Ethan") | |
pila.imprimir() | |
pila.desapilar() | |
pila.imprimir() | |
pila.apilar("Leon Scott Kennedy") | |
pila.imprimir() | |
pila.desapilar() | |
pila.desapilar() | |
pila.imprimir() | |
pila.desapilar() | |
pila.desapilar() | |
pila.imprimir() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment