/** Initialize your data structure here. */ publicMyStack() { queue1 = newLinkedList<>(); queue2 = newLinkedList<>(); }
/** Push element x onto stack. */ publicvoidpush(int x) { queue1.add(x); }
/** Removes the element on top of the stack and returns that element. */ publicintpop() { while (queue1.size() > 1) { queue2.add(queue1.remove()); } Queue<Integer> temp = queue1; queue1 = queue2; queue2 = temp; return queue2.remove(); }
/** Get the top element. */ publicinttop() { while (queue1.size() > 1) { queue2.add(queue1.remove()); } inttopElement= queue1.peek(); queue2.add(queue1.remove()); Queue<Integer> temp = queue1; queue1 = queue2; queue2 = temp; return topElement; }
/** Returns whether the stack is empty. */ publicbooleanempty() { if (queue1.isEmpty() && queue2.isEmpty()) { returntrue; } else { returnfalse; } } }
/** * 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(); */
/** Initialize your data structure here. */ publicMyStack() { queue = newLinkedList<Integer>(); }
/** Push element x onto stack. */ publicvoidpush(int x) { queue.add(x); for (inti=0; i < queue.size() - 1; i++) queue.add(queue.poll()); }
/** Removes the element on top of the stack and returns that element. */ publicintpop() { return queue.poll(); }
/** Get the top element. */ publicinttop() { return queue.peek(); }
/** Returns whether the stack is empty. */ publicbooleanempty() { return queue.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(); */