@@ -0,0 +1,1803 @@
+import java.util.ArrayDeque;%0Aimport java.util.Deque;%0Aimport java.util.LinkedList;%0Aimport java.util.Queue;%0A%0A/**%0A * Implement the following operations of a stack using queues.%0A * %3Cp%3E%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%0A * 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%0A * (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%0A * stack).%0A * Update (2015-06-11):%0A * The class name of the Java function had been updated to MyStack instead of Stack.%0A * %3Cp%3E%0A * Tags: Stack, Design%0A * Similar Problems: (E) Implement Queue using Stacks%0A */%0Apublic class ImplementStackUsingQueues %7B%0A%0A    /**%0A     * Use queue%0A     * It would too easy to use Deque%0A     */%0A    class MyStack %7B%0A%0A        Queue%3CInteger%3E queue = new LinkedList%3C%3E();%0A%0A        // Push element x onto stack.%0A        public void push(int x) %7B%0A            queue.offer(x);%0A            for (int i = 0; i %3C queue.size() - 1; i++) %7B%0A                queue.offer(queue.poll());%0A            %7D%0A        %7D%0A%0A        // Removes the element on top of the stack.%0A        public void pop() %7B%0A            queue.poll();%0A        %7D%0A%0A        // Get the top element.%0A        public int top() %7B%0A            return queue.peek();%0A        %7D%0A%0A        // Return whether the stack is empty.%0A        public boolean empty() %7B%0A            return queue.isEmpty();%0A        %7D%0A    %7D%0A%7D%0A

add Implement Stack Using Queues