Stack
class EmptyStackException(Exception):
    pass

class Element:
    def __init__(self, value, next):
        self.value = value
        self.next = next

class Stack:
    def __init__(self):
        self.head = None

    
    def push(self, element):
        self.head = Element(element, self.head)

    
    def pop(self):
        if self.empty(): raise EmptyStackException
        result = self.head.value
        self.head = self.head.next
        return result

    
    def empty(self):
        return self.head == None


if __name__ == "__main__":
    
    stack = Stack()
    elements = ["first", "second", "third", "fourth"]
    for e in elements:
        stack.push(e)

    result = []
    while not stack.empty():
        result.append(stack.pop())

    assert result == ["fourth", "third", "second", "first"]

相关文章:

  • 2021-05-10
  • 2021-08-11
  • 2022-12-23
  • 2022-01-19
  • 2022-02-19
  • 2021-05-06
  • 2022-12-23
猜你喜欢
  • 2021-11-01
  • 2021-07-27
  • 2021-12-22
  • 2021-12-26
  • 2021-11-09
  • 2022-02-22
相关资源
相似解决方案