Implement Stack using Queues


Using java dequeue to solve this!
public class MyStack {Deque<Integer> queue; /** Initialize your data structure here. */ public MyStack() { queue = new LinkedList<Integer>(); } /** Push element x onto stack. */ public void push(int x) { queue.offerFirst(x); } /** Removes the element on top of the stack and returns that element. */ public int pop() { if (!queue.isEmpty()) { return queue.pollFirst(); } return 1; } /** Get the top element. */ public int top() { if (!queue.isEmpty()) { return queue.peekFirst(); } return 1; } /** Returns whether the stack is empty. */ public boolean empty() { return queue.isEmpty(); }
}

@sirykd Actually the Approach #3 is the samilar with Approach #2 as just replacing the 2 Queues with the only one common shared Queue1 like a Ring. So the Approach #1 can also replacing the 2 Queues with the only one common shared Queue1 like a Ring as your Approach.