队列的特性是:“先入先出”,栈的特性是:“先入后出”
思路:固定栈1入队,栈2出队。pop() 操作时,

  • (1)如果两栈都为空,报异常;
  • (2)如果出队栈有元素就出队;
  • (3)如果出队栈为空,就把入队栈的元素都弹过来再出队。
import java.util.Stack;
public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();

    public void push(int node) {
        stack1.push(node);
    }

    public int pop() {
        while(stack1.isEmpty() && stack2.isEmpty()){
            throw new RuntimeException("Queue is empty");
        }
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }   
}