使用双栈实现队列

public class TwoStarkQueue {
    public Stack<Integer> popStack;
    public Stack<Integer> pushStack;

    public TwoStarkQueue() {
        this.popStack = new Stack<>();
        this.pushStack = new Stack<>();
    }

    /**
     * 将push栈中的数据全部倒进pop栈
     */
    private void pushToPop(){
        if (popStack.isEmpty()){
            while (!pushStack.isEmpty()){
                popStack.add(pushStack.pop());
            }
        }
    }

    public void pull(int val){
        pushStack.add(val);
        pushToPop();
    }

    public Integer pop(){
        if (popStack.isEmpty()){
            System.out.println("该栈为空!");
            return null;
        }
        pushToPop();
        return popStack.pop();
    }
}

使用双队列实现栈

/**
 * 使用两个队列实现栈的操作
 * 每次弹出时 都 将队列的最后一个弹出即可
 * 然后再讲两个队列的引用交换
 */
public class TwoQueueStack {

    public Queue<Integer> queue;
    public Queue<Integer> help;


    public TwoQueueStack() {
        this.queue = new LinkedList<>();
        this.help = new LinkedList<>();
    }

    public void offer(Integer pullInt){
        queue.offer(pullInt);
    }

    public Integer poll(){
        while (queue.size() > 1){
            help.offer(queue.poll());
        }
        Integer ans = queue.poll();
        Queue<Integer> temp = queue;
        queue = help;
        help = temp;
        return ans;
    }

    public Integer peek(){
        while (queue.size() > 1){
            help.offer(queue.poll());
        }
        Integer ans = queue.peek();
        Queue<Integer> temp = queue;
        queue = help;
        help = temp;
        return ans;
    }
}