@@ -0,0 +1,1867 @@
+/*%0A Implement the following operations of a queue using stacks.%0A%0A    push(x) -- Push element x to the back of queue.%0A    pop() -- Removes the element from in front of queue.%0A    peek() -- Get the front element.%0A    empty() -- Return whether the queue is empty.%0A%0ANotes:%0A%0A    You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.%0A    Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.%0A    You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).%0A%0A*/%0A%0Aimport java.util.*;%0Aimport java.io.*;%0A%0Apublic class ImplementQueueusingStacks %7B%0A%09public static class MyQueue %7B%0A%09%09Stack%3CInteger%3E queue = new Stack%3CInteger%3E();%0A%09%09Stack%3CInteger%3E cp = new Stack%3CInteger%3E();%0A        // Push element x to the back of queue.%0A%09%09public void push(int x) %7B%0A%09%09%09while(!cp.empty())%7B%0A%09%09%09%09queue.push(cp.pop());%0A%09%09%09%7D%0A%09%09%09queue.push(x);%0A%09%09%09while(!queue.empty())%7B%0A%09%09%09%09cp.push(queue.pop());%0A%09%09%09%7D%09%0A        %0A%09%09%7D%0A%0A%09%09// Removes the element from in front of queue.%0A%09%09public void pop() %7B%0A%09%09    while(!queue.empty())%7B%0A%09%09        queue.pop();%0A%09%09%09%7D%0A%09        cp.pop();%0A%09%09    while(!cp.empty())%7B%0A%09%09%09    queue.push(cp.pop());%0A%09%09    %7D%0A%09%09%09while(!queue.empty())%7B%0A%09%09%09%09cp.push(queue.pop());%0A%09%09    %7D%0A%09%09%7D%0A%0A%09    // Get the front element.%0A%09%09public int peek() %7B%0A%09%09%09return cp.peek();%0A%09    %7D%0A%0A%09    // Return whether the queue is empty.%0A%09%09public boolean empty() %7B%0A%09%09%09return cp.size() == 0? true: false;%0A%09%09%7D%0A%09%7D%0A%0A%09%0A%09public static void main(String%5B%5D args) %7B%0A%09%09MyQueue q = new MyQueue();%0A%09%09q.push(5);%0A%09%09q.push(4);%0A%09%09q.push(3);%0A%09%09System.out.println(%22current top is %22 + q.peek());%0A%09%09System.out.println(%22is empty?  %22 + q.empty());%0A%09%09return;%0A%09%7D%09%0A%7D%0A

implement queue using stacks