Implement Stack using Queues
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack
(pushtoppopempty
Implement the MyStack
-
Pushes elementvoid push(int x) to the top of the stack.x -
Removes the element on the top of the stack and returns it.int pop() -
Returns the element on the top of the stack.int top() -
Returnsboolean empty() if the stack is empty,true otherwise.false
-
You must use only standard operations of a queue, which means that only
,push to back ,peek/pop from front andsize operations are valid.is empty - Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
Example 1
Output: [null, null, null, 2, 2, false]
Explanation:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // return 2
myStack.pop(); // return 2
myStack.empty(); // return False
Constraints:
1 <= x <= 9- At most
calls will be made to100 ,push ,pop , andtop .empty - All the calls to
andpop are valid.top
Follow-up: Can you implement the stack using only one queue?
Solution 1 - Implementation of stack using one queue
In this approach, we are going to implement a StackQueue
-
: we will add the elementpush(int x) to the queue.x -
: we will go through stack elements frompop() and remove these elements from top, and add it back to the stack. Say if the stack looks like this1.....n-11 2 3  .. .. , then after after popping elements and add it back, it looks like thisn-13 2 1  .. .. , now we can simply pop the first element from the queue, it returns , note that pop() operation will also remove and return the element.1 -
: this operation is similar totop() , except at the end, we will make apop() operation on queue, it returns the first element from the queue, but will not remove it.peek() -
: we will simply callempty() , this returnsqueue.isEmpty() is the queue is empty,true otherwise.false
import java.util.Deque;
import java.util.LinkedList;
public class StackUsingQueue {
final Deque<Integer> queue;
public StackUsingQueue() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.push(x);
}
public int pop() {
for(int i=0; i<queue.size()-1; i++) {
queue.push(queue.pop());
}
return queue.pop();
}
public int top() {
for(int i=0; i<queue.size()-1; i++) {
queue.push(queue.pop());
}
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
public static void main(String[] args) {
StackUsingQueue stackUsingQueue = new StackUsingQueue();
stackUsingQueue.push(3);
stackUsingQueue.push(2);
stackUsingQueue.push(1);
System.out.println("top : " + stackUsingQueue.top());
System.out.println(stackUsingQueue.pop());
System.out.println(stackUsingQueue.pop());
System.out.println(stackUsingQueue.pop());
System.out.println(stackUsingQueue.empty());
}
}
Complexity Analysis:
Time complexity: In this implementation push(int x)empty()pop()top()
Space complexity: O(n).
Above implementations source code can be found at