码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-01-13 07:06:32      阅读:227      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!