用两个栈实现队列

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

public class StackQueue {
    Stack stack = new Stack<>();
    Stack 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();
    }
}

你可能感兴趣的:(用两个栈实现队列)