用两个栈实现队列

2018-06-08  本文已影响19人  暮染1

用两个栈来实现一个队列,完成队列的Push和Pop操作。

public class StackQueue {
    Stack<String> stack = new Stack<>();
    Stack<String> stack2 = new Stack<>();

    public void push(String content) {
        stack.push(content);
    }

    public String pop() {
        if (stack2.empty()) {
            while (!stack.empty())
                stack2.push(stack.pop());
        }
        return stack2.pop();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读