📘
Implement Stack using Queues
EasyStacks & Queues
Implement a LIFO stack using only two queues. Support push, pop, top, and empty.
Output results of pop, top, and empty operations.
Example:
Input:
push,1;push,2;top;pop;empty
Output:
2 2 False
Reasoning:
- We start with an empty stack and perform the given operations in sequence:
push,1andpush,2, resulting in a stack with elements 1 and 2. - The
topoperation returns the last element added to the stack, which is 2. - The
popoperation removes the last element added to the stack, which is also 2, leaving the stack with only element 1. - The
emptyoperation checks if the stack is empty, which it is not, since it still contains element 1, so it returnsFalse.
Constraints:
- 1 <= val <= 9
- At most 100 operations
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.