队列实现栈 class MyQueue { private Stack<Integer> a; private Stack<Integer> b; public MyQueue{ a = new Stack<>(); b = new Stack<>(); } public void Push(int x) { a.push(x); } public int pop(){ if (b.isEmpty()) { while (!a.isEmpty()) { b.push(a.pop()); } } return b.pop(); } public int peek(){ if (b.isEmpty()) { while (!a.isEmpty()) { b.push(a.pop()); } } return b.peek(); } public boolean isEmpty(){ return a.isEmpty() && b.isEmpty() } } 栈实现队列 class MyStack { Queue<Integer> queue; public MyQueue{ queue = new LinkedList<>(); } public void Push(int x) { queue.add(x); for(int i = 0; i < queue.size() - 1; i++){ queue.add(queue.poll()); } } public int pop(){ return queue.poll(); } public int peek(){ return queue.peek(); } public boolean isEmpty(){ return queue.peek(); } }