How to implement Queue using Stack?

Suraj Said

+0 -0

One approach to implement a queue using two stacks is as follows:
i) Initialize two stacks: stack1 and stack2.
ii) For each enqueues operation, push the new element to stack1.
iii) For each dequeue operation, transfer all elements from stack1 to stack2 if stack2 is empty. The last element in stack2 is removed and returned.

Submit Your Response