Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

Notes: Input: ["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []]
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:

Follow-up: Can you implement the stack using only one queue?

Contents

In this approach, we are going to implement a Stack datastructure using a single Queue, as we know stack works in LIFO (last in first out) order, but the queue works in FIFO (first in first out) order. So, we will implement a stack operations as follows:

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) and empty() runs in constant time, and pop() and top() runs in O(n) time, because we are iterating through stack to move the elements from the beginning to the end of queue.
Space complexity: O(n).

Above implementations source code can be found at GitHub link for Java code