Implement Queue using Stacks

来源:互联网 发布:mysql 命令行导入 编辑:程序博客网 时间:2024/06/05 04:10

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.
Notes:
  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue)ja

java
class MyQueue {    /** Initialize your data structure here. */    Stack<Integer> stack1;    Stack<Integer> stack2;    public MyQueue() {        stack1 = new Stack<Integer>();        stack2 = new Stack<Integer>();    }    private void transfer(Stack<Integer> stack1, Stack<Integer> stack2) {        while(!stack1.empty()) {            stack2.push(stack1.pop());        }    }    /** Push element x to the back of queue. */    public void push(int x) {        stack1.push(x);    }        /** Removes the element from in front of queue and returns that element. */    public int pop() {        if (stack2.empty()) {            transfer(stack1, stack2);        }        return stack2.pop();    }        /** Get the front element. */    public int peek() {        if (stack2.empty()) {            transfer(stack1, stack2);        }        return stack2.peek();    }        /** Returns whether the queue is empty. */    public boolean empty() {        if (stack2.empty()) {            transfer(stack1, stack2);        }        return stack2.empty();    }}/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
python
class MyQueue(object):    def __init__(self):        """        Initialize your data structure here.        """        self.stack1 = []        self.stack2 = []        def transfer(self):        while len(self.stack1) != 0:            self.stack2.append(self.stack1.pop())            def push(self, x):        """        Push element x to the back of queue.        :type x: int        :rtype: void        """        self.stack1.append(x)    def pop(self):        """        Removes the element from in front of queue and returns that element.        :rtype: int        """        if len(self.stack2) == 0:            self.transfer()        return self.stack2.pop()    def peek(self):        """        Get the front element.        :rtype: int        """        if len(self.stack2) == 0:            self.transfer()        return self.stack2[-1]    def empty(self):        """        Returns whether the queue is empty.        :rtype: bool        """        if len(self.stack2) == 0:            self.transfer()        return len(self.stack2) == 0# Your MyQueue object will be instantiated and called as such:# obj = MyQueue()# obj.push(x)# param_2 = obj.pop()# param_3 = obj.peek()# param_4 = obj.empty()