225. 用队列实现栈

225. 用队列实现栈

 

方法一

class MyStack(object):

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

    def push(self, x):
        """
        Push element x onto stack.
        :type x: int
        :rtype: void
        """
        self.stack.append(x)

    def pop(self):
        """
        Removes the element on top of the stack and returns that element.
        :rtype: int
        """
        if len(self.stack) == 0:
            return None
        return self.stack.pop()

    def top(self):
        """
        Get the top element.
        :rtype: int
        """
        if len(self.stack) == 0:
            return None
        val = self.stack[-1]
        return val

    def empty(self):
        """
        Returns whether the stack is empty.
        :rtype: bool
        """
        if len(self.stack) == 0:
            return True
        else:
            return False


# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()

 

posted @ 2019-01-21 10:49  小学弟-  阅读(128)  评论(0编辑  收藏  举报