Queue Using Two Stacks
Mediumjavapythonccppjavascript
Implement a queue using only two stacks, so that operations still behave in First In, First Out order.
Input Format
The first line contains an integer Q. Each of the next Q lines is one of: enqueue X, dequeue, front, empty.
Output Format
For each dequeue or front, print the element (or -1 if empty). For each empty, print 1 if empty, otherwise 0. One value per line.
Example 1
Input
4 enqueue 10 enqueue 20 dequeue front
Output
10 20
Explanation: The first inserted element is removed first.
- 1 <= Q <= 100000
- -1000000000 <= X <= 1000000000
Submit an attempt to unlock the solution. (Sign in first.)
Related Problems
- Next Greater ElementMedium · java, python, c, cpp, javascriptSolve Problem
- Next Smaller ElementMedium · java, python, c, cpp, javascriptSolve Problem
- Min StackMedium · java, python, c, cpp, javascriptSolve Problem