@@ -0,0 +1,1702 @@
+import java.util.Stack;%0A%0A/**%0A * Implement the following operations of a queue using stacks.%0A *%0A * push(x) -- Push element x to the back of queue.%0A *%0A * pop() -- Removes the element from in front of queue.%0A *%0A * peek() -- Get the front element.%0A *%0A * empty() -- Return whether the queue is empty.%0A *%0A * Notes:%0A *%0A * * You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is%0A * empty operations are valid.%0A *%0A * * Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque%0A * (double-ended queue), as long as you use only standard operations of a stack.%0A *%0A * * You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty%0A * queue).%0A *%0A * Tags: Stack, Design%0A *%0A * Similar Problems: (E) Implement Stack using Queues%0A */%0Apublic class ImplementingQueueWithStacks %7B%0A%0A    class MyQueue %7B%0A%0A        Stack%3CInteger%3E input = new Stack%3C%3E();%0A        Stack%3CInteger%3E output = new Stack%3C%3E();%0A%0A%0A        // Push element x to the back of queue.%0A        public void push(int x) %7B%0A            input.push(x);%0A        %7D%0A%0A        // Removes the element from in front of queue.%0A        public void pop() %7B%0A            peek();%0A            output.pop();%0A        %7D%0A%0A        // Get the front element.%0A        public int peek() %7B%0A            if (!output.isEmpty()) %7B%0A                while (!input.isEmpty())%0A                    output.push(input.pop());%0A            %7D%0A            return output.peek();%0A        %7D%0A%0A        // Return whether the queue is empty.%0A        public boolean empty() %7B%0A            return input.isEmpty() && output.isEmpty();%0A        %7D%0A    %7D%0A%7D%0A

add 232 Implementing Queue with Stack