225. Implement Stack using Queues
Easy
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Example:
MyStack stack = new MyStack(); stack.push(1); stack.push(2); stack.top(); // returns 2 stack.pop(); // returns 2 stack.empty(); // returns false
Notes:
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
package leetcode.easy; public class ImplementStackUsingQueues { @org.junit.Test public void test1() { MyStack1 stack = new MyStack1(); stack.push(1); stack.push(2); System.out.println(stack.top()); // returns 2 System.out.println(stack.pop()); // returns 2 System.out.println(stack.empty()); // returns false } @org.junit.Test public void test2() { MyStack2 stack = new MyStack2(); stack.push(1); stack.push(2); System.out.println(stack.top()); // returns 2 System.out.println(stack.pop()); // returns 2 System.out.println(stack.empty()); // returns false } @org.junit.Test public void test3() { MyStack3 stack = new MyStack3(); stack.push(1); stack.push(2); System.out.println(stack.top()); // returns 2 System.out.println(stack.pop()); // returns 2 System.out.println(stack.empty()); // returns false } } class MyStack1 { private java.util.Queue<Integer> q1 = new java.util.LinkedList<>(); private java.util.Queue<Integer> q2 = new java.util.LinkedList<>(); private int top; /** Initialize your data structure here. */ public MyStack1() { } // Push element x onto stack. public void push(int x) { q1.add(x); top = x; } // Removes the element on top of the stack. public int pop() { while (q1.size() > 1) { top = q1.remove(); q2.add(top); } int tempPop = q1.remove(); java.util.Queue<Integer> temp = q1; q1 = q2; q2 = temp; return tempPop; } /** Get the top element. */ public int top() { return top; } /** Returns whether the stack is empty. */ public boolean empty() { return q1.isEmpty(); } } class MyStack2 { private java.util.Queue<Integer> q1 = new java.util.LinkedList<>(); private java.util.Queue<Integer> q2 = new java.util.LinkedList<>(); private int top; /** Initialize your data structure here. */ public MyStack2() { } /** Push element x onto stack. */ public void push(int x) { q2.add(x); top = x; while (!q1.isEmpty()) { q2.add(q1.remove()); } java.util.Queue<Integer> temp = q1; q1 = q2; q2 = temp; } // Removes the element on top of the stack. public int pop() { int temp = q1.remove(); if (!q1.isEmpty()) { top = q1.peek(); } return temp; } // Get the top element. public int top() { return top; } // Return whether the stack is empty. public boolean empty() { return q1.isEmpty(); } } class MyStack3 { private java.util.Queue<Integer> q1 = new java.util.LinkedList<>(); private int top; /** Initialize your data structure here. */ public MyStack3() { } // Push element x onto stack. public void push(int x) { q1.add(x); int sz = q1.size(); while (sz > 1) { q1.add(q1.remove()); sz--; } top = x; } // Removes the element on top of the stack. public int pop() { int temp = q1.remove(); if (!q1.isEmpty()) { top = q1.peek(); } return temp; } // Get the top element. public int top() { return top; } // Return 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(); */