stack_use_queue

  • 2022-12-14
  • 浏览 (534)

stack_use_queue.py 源码

# 使用队列实现一个栈

class MyStack:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.input = []

    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        self.input.append(x)
        if len(self.input) > 0:
            self.input = [x] + self.input[:len(self.input) - 1]

    def pop(self) -> int:
        """
        Removes the element on top of the stack and returns that element.
        """
        return self.input.pop(0)

    def top(self) -> int:
        """
        Get the top element.
        """
        return self.input[0]


    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        return len(self.input) == 0

你可能感兴趣的文章

array_queue

circular_deque

circular_queue

0  赞