A way to reverse queue using only two temporary queues and nothing more?
algorithm, data-structures
Solution
You can:
- Use the two queues to simulate a stack.
- Push all of the elements of the original queue to this stack.
- Now pop each element from the stack and add it to the original queue.
Problem
Is there a way to reverse items' order in queue using only two temporary queues (and no other variables, such as counters)? Only standard queue operation are available: ENQUEUE(e), DEQUEUE(), EMPTY()? Solutions in any language or pseudocode are welcome.