Question - How would you implement a queue using a stack?
Answer -
Using two stacks, you can implement a queue. The purpose is to complete the queue's en queue operation so that the initially entered element always ends up at the top of the stack.
- First, to enqueue an item into the queue, migrate all the elements from the beginning stack to the second stack, push the item into the stack, and send all elements back to the first stack.
- To dequeue an item from the queue, return the top item from the first stack.