Queue using two stacks · Compiler
Queue using two stacks
Implement a FIFO queue with two initially empty stacks. ADD x pushes x onto the input stack. For TAKE, if the output stack is empty, move every item from input to output by popping and pushing one at a time. Then pop the output stack if possible. For each TAKE, report the item removed and how many items crossed between stacks during that command.
- Input
- The first line contains the command count q (1 <= q <= 100). Each of the next q lines contains ADD x or TAKE. Values are integers from 1 to 1000. There is at least one TAKE.
- Output
- For each TAKE print the removed value followed by the transfer count on the same line, separated by a space. For an empty queue print EMPTY 0.
Compiler Explorer runs your code when you run or check it.
Loading your exercise draft…