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:
void push(int x) Pushes element x to the top of the stack.
int pop() Removes the element on the top of the stack and returns it.
int top() Returns the element on the top of the stack.
boolean empty() Returns true if the stack is empty, false otherwise.
Notes:
You must use only standard operations of a queue, which means only push to back, peek/pop from front, size, and is empty operations are valid.
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.
Follow-up: Can you implement the stack such that each operation is amortizedO(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.
classMyStack{ Queue<Integer> a; Queue<Integer> b; /** Initialize your data structure here. */ publicMyStack(){ a = new ArrayDeque<>(); b = new ArrayDeque<>(); } /** Push element x onto stack. */ publicvoidpush(int x){ Queue<Integer> full; Queue<Integer> empty; if(!a.isEmpty()) { full = a; empty = b; } else { full = b; empty = a; } full.offer(x); } /** Removes the element on top of the stack and returns that element. */ publicintpop(){ Queue<Integer> full; Queue<Integer> empty; if(!a.isEmpty()) { full = a; empty = b; } else { full = b; empty = a; } while(full.size() > 1) { int tmp = full.poll(); empty.offer(tmp); } return full.poll(); } /** Get the top element. */ publicinttop(){ Queue<Integer> full; Queue<Integer> empty; if(!a.isEmpty()) { full = a; empty = b; } else { full = b; empty = a; } while(full.size() > 1) { int tmp = full.poll(); empty.offer(tmp); } int ans = full.peek(); empty.offer(full.poll()); return ans; } /** Returns whether the stack is empty. */ publicbooleanempty(){ return a.isEmpty() && b.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(); */
classMyStack{ Queue<Integer> a; Queue<Integer> b; /** Initialize your data structure here. */ publicMyStack(){ a = new ArrayDeque<>(); b = new ArrayDeque<>(); } /** Push element x onto stack. */ publicvoidpush(int x){ Queue<Integer> full; Queue<Integer> empty; if(!a.isEmpty()) { full = a; empty = b; } else { full = b; empty = a; } empty.offer(x); while(!full.isEmpty()) { empty.offer(full.poll()); } } /** Removes the element on top of the stack and returns that element. */ publicintpop(){ Queue<Integer> full; Queue<Integer> empty; if(!a.isEmpty()) { full = a; empty = b; } else { full = b; empty = a; } return full.poll(); } /** Get the top element. */ publicinttop(){ Queue<Integer> full; Queue<Integer> empty; if(!a.isEmpty()) { full = a; empty = b; } else { full = b; empty = a; } return full.peek(); } /** Returns whether the stack is empty. */ publicbooleanempty(){ return a.isEmpty() && b.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(); */
classMyStack{ Queue<Integer> a; /** Initialize your data structure here. */ publicMyStack(){ a = new ArrayDeque<>(); } /** Push element x onto stack. */ publicvoidpush(int x){ int size = a.size(); a.offer(x); while(size > 0) { a.offer(a.poll()); --size; } } /** Removes the element on top of the stack and returns that element. */ publicintpop(){ return a.poll(); } /** Get the top element. */ publicinttop(){ return a.peek(); } /** Returns whether the stack is empty. */ publicbooleanempty(){ return a.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(); */