@@ -0,0 +1,1497 @@
+package leetcode221_230;%0A%0Aimport java.util.LinkedList;%0A%0A/**Implement the following operations of a stack using queues.%0A push(x) -- Push element x onto stack.%0A pop() -- Removes the element on top of the stack.%0A top() -- Get the top element.%0A empty() -- Return whether the stack is empty.%0A Notes:%0A You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.%0A Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.%0A You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).%0A * Created by eugene on 16/4/19.%0A */%0Apublic class ImplementStackUsingQueues %7B%0A%0A    //%E4%BB%85%E5%80%9F%E5%8A%A9%E4%B8%80%E4%B8%AA%E9%98%9F%E5%88%97,%E4%BD%86%E6%98%AFpush%E6%93%8D%E4%BD%9C%E6%85%A2%0A    class MyStack %7B%0A%0A        LinkedList%3CInteger%3E q;%0A        public MyStack() %7B%0A            q = new LinkedList%3C%3E();%0A        %7D%0A%0A        // Push element x onto stack.%0A        public void push(int x) %7B%0A            q.offer(x);%0A            for (int i=0; i%3Cq.size()-1; i++) q.offer(q.poll());%0A        %7D%0A%0A        // Removes the element on top of the stack.%0A        public void pop() %7B%0A            q.poll();%0A        %7D%0A%0A        // Get the top element.%0A        public int top() %7B%0A            return q.peek();%0A        %7D%0A%0A        // Return whether the stack is empty.%0A        public boolean empty() %7B%0A            return q.isEmpty();%0A        %7D%0A    %7D%0A%0A%7D%0A

Implement Stack using Queues