代码如下:
class MyStack { // Push element x onto stack. Queue<Integer> queue=new ArrayDeque<>(); public void push(int x) { Queue<Integer> p=new ArrayDeque<>(); int c=queue.size(); while(c!=0) { p.offer(queue.peek()); queue.remove(); c--; } queue.offer(x); int d=p.size(); while(d!=0) { queue.offer(p.peek()); p.remove(); d--; } } // Removes the element on top of the stack. public void pop() { queue.remove(); } // Get the top element. public int top() { return queue.peek(); } // Return whether the stack is empty. public boolean empty() { if(queue.size()==0) return true; return false; } }