代码:

class MyQueue {
    // Push element x to the back of queue.
    Stack<Integer> stack = new Stack<>();
    Stack<Integer> aux = new Stack<>();
    public void push(int x) {
        while(!stack.isEmpty()){
            aux.push(stack.pop());
        }
        stack.push(x);
        while(!aux.isEmpty()){
            stack.push(aux.pop());
        }
    }

    // Removes the element from in front of queue.
    public void pop() {
        stack.pop();
    }

    // Get the front element.
    public int peek() {
        return stack.peek();
    }

    // Return whether the queue is empty.
    public boolean empty() {
        return stack.isEmpty();
    }
}

  

posted on 2016-01-13 05:31  爱推理的骑士  阅读(111)  评论(0编辑  收藏  举报