class StackNode(object): def __init__(self, value, nxt): self.value = value self.next = nxt def __repr__(self): nval = self.next and self.next.value or None return f"[{self.value}:{repr(nval)}]" class Stack(object): def __init__(self): self.topmost = None def push(self, obj): """ Pushes a new value to the top of the stack. Añade un nuevo valor en el tope del stack. """ if not self.topmost: self.topmost = StackNode(obj, None) else: self.topmost = StackNode(obj, self.topmost) def pop(self): """ Pops the value that is currently on the top of the stack. Remueve el valor que está en el tope del stack. """ if self.topmost: node = self.topmost if self.topmost.next: self.topmost = self.topmost.next else: self.topmost = None return node.value def top(self): """ Returns the reference to the first item, does not remove. Regresa la referencia al primer ítem sin removerlo. """ if self.topmost: return self.topmost.value def count(self): """ Returns number of items. Regresa la cantidad de ítems. """ count = 0 if self.topmost: node = self.topmost while node: count += 1 node = node.next return count def dump(self, mark="----"): """ Debugging function that dumps the contents of the stack. Función de depuración que arroja los contenidos del stack. """ if self.topmost: node = self.topmost print(node.value) while node.next: node = node.next print(mark) print(node.value)