[剑指offer]用两个栈实现队列

[剑指offer]用两个栈实现队列
思路:
进行队列的push操作时,直接将元素push进stack1即可,Pop操作先判断stack2中有无元素,如果有直接pop,没有就将stack1中元素pop并push进stack2中,再进行pop操作即可
实现:

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() {
        if(stack2.isEmpty())
            while(!stack1.isEmpty()){
                int temp=stack1.pop();
                stack2.push(temp);
            }
        return stack2.pop();
    }
}