You are viewing a single comment's thread. Return to all comments →
A naive solution will be to push in Stack1 for every enqueue and for a dequeue, pop all elements from Stack1, push them to Stack2. Now pop the top of Stack2 and transfer back all elements to Stack1. To do the job efficiently, -we do not transfer the elements back after the first dequeue. -Here after, Enqueue is done in Stack1 and pop() and dequeue() is performed on Stack2 until Stack2 is empty. -Only when Stack2 is empty, we again transfer all elements from Stack1 to Stack2 and continue the process.
Queues: A Tale of Two Stacks
You are viewing a single comment's thread. Return to all comments →