# 225-implement-stack-using-queues Try it on leetcode ## Description

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (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 using only one queue?

## Solution(Python) ```Python class MyStack: def __init__(self): self._queue = collections.deque() def push(self, x): q = self._queue q.append(x) for _ in range(len(q) - 1): q.append(q.popleft()) def pop(self): return self._queue.popleft() def top(self): return self._queue[0] def empty(self): return not len(self._queue) # Your MyStack object will be instantiated and called as such: # obj = MyStack() # obj.push(x) # param_2 = obj.pop() # param_3 = obj.top() # param_4 = obj.empty() ```