Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 28, 2021 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/1449f4dbb09550d487635fe2b33a512e to your computer and use it in GitHub Desktop.
Save parzibyte/1449f4dbb09550d487635fe2b33a512e to your computer and use it in GitHub Desktop.
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("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment