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 queue (push, top, pop, and empty).

Implement the MyStack class:

Notes:

 

Example 1:

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 such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer. You can use more than two queues.

Solution:

1. two queues, always push to empty queue, to that the empty queue's first element is the one we want to pop. push other queue's elements to the tail of the empty queue as well
class MyStack {

    /** Initialize your data structure here. */
    
    Queue<Integer> q1;
    Queue<Integer> q2;
    
    public MyStack() {
        q1 = new ArrayDeque();
        q2 = new ArrayDeque();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        if (q1.isEmpty()) {
            q1.offer(x);
            while (!q2.isEmpty()) {
                q1.offer(q2.poll());
            }
        } else {
            q2.offer(x);
            while (!q1.isEmpty()) {
                q2.offer(q1.poll());
            }
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if (!q2.isEmpty()) {
            return q2.poll();
        }
        return q1.poll();
    }
    
    /** Get the top element. */
    public int top() {
        if (!q2.isEmpty()) {
            return q2.peek();
        }
        return q1.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return q1.isEmpty() && q2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

2. always append to non empty queue, so the end of that queue is the result. when popping, move the elements before result to the other queue, and return the last element

class MyStack {

    /** Initialize your data structure here. */
    
    Queue<Integer> q1;
    Queue<Integer> q2;
    
    public MyStack() {
        q1 = new ArrayDeque();
        q2 = new ArrayDeque();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        if (!q1.isEmpty()) {
            q1.offer(x);
        } else {
            q2.offer(x);
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if (!q1.isEmpty()) {
            int size = q1.size();
            for (int i = 0; i < size - 1; i ++) {
                q2.offer(q1.poll());
            }
            return q1.poll();
        } else {
            int size = q2.size();
            for (int i = 0; i < size - 1; i ++) {
                q1.offer(q2.poll());
            }
            return q2.poll();            
        }
    }
    
    /** Get the top element. */
    public int top() {
        if (!q1.isEmpty()) {
            int size = q1.size();
            for (int i = 0; i < size - 1; i ++) {
                q2.offer(q1.poll());
            }
            int res = q1.poll();
            q2.offer(res);
            return res;
        } else {
            int size = q2.size();
            for (int i = 0; i < size - 1; i ++) {
                q1.offer(q2.poll());
            }
            int res = q2.poll();
            q1.offer(res);
            return res;
        }
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return q1.isEmpty() && q2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

3. one queue

class MyStack {

    /** Initialize your data structure here. */
    
    Queue<Integer> q1;
    
    public MyStack() {
        q1 = new ArrayDeque();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        q1.offer(x);
        for (int i = 0; i < q1.size() - 1; i ++) {
            q1.offer(q1.poll());
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return q1.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return q1.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return q1.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */