队列的push和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() {
        //把节点都转存到队列2中
    while(!stack1.isEmpty())
    {
        stack2.push(stack1.pop());
    }
        //在队列2中取出一个节点
        int num = stack2.pop();
        //在把节点恢复到队列1中
        while(!stack2.isEmpty())
        {
            stack1.push(stack2.pop());
        }
        return num;
    }
}

你可能感兴趣的:(队列)