首页 > 其他 > 详细

Jan 12 - Implement Queue using Stacks; Stack; Queue Implementation;

时间:2016-01-13 07:03:50      阅读:209      评论:0      收藏:0      [点我收藏+]

代码:

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();
    }
}

  

Jan 12 - Implement Queue using Stacks; Stack; Queue Implementation;

原文:http://www.cnblogs.com/5683yue/p/5126125.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!