📘
Implement Queue using Stacks
EasyDesign & Stacks
Implement a FIFO queue using only two stacks. Support push, pop (remove front), peek (get front), and empty.
Output the result of pop, peek, and empty operations.
Example:
Input:
push,1;push,2;peek;pop;empty
Output:
1 1 False
Reasoning:
- We start with an empty queue and apply the given operations in sequence:
push,1andpush,2add elements 1 and 2 to the queue. - The
peekoperation returns the front element of the queue, which is 1, since the queue is implemented as a FIFO data structure. - The
popoperation removes the front element from the queue, which is also 1, and returns it. - The
emptyoperation checks if the queue is empty after thepopoperation; since there is still one element (2) left in the queue, it returnsFalse.
Constraints:
- 1 <= val <= 9
- At most 100 operations
- All pop and peek calls are valid
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.