Implement Queue Using Stacks
Objective: We know that Queue is FIFO (First-in-First-Out) and Stack is LIFO ( Last-in-First-Out).
Here our objective is to implement queue using stacks.
- Take 2 Stacks, stack1 and stack2.
- stack1 will be used a back of the Queue and stack2 will be used as front of the Queue.
- Push() operation will be done on stack1, and peek() and pop() operations will be done on stack2.
- When peek() and pop() are called, check is stack2 is empty, if yes then move all the elements from stack1 and push them into stack2.
POP from Queue 10
Top Companies Interview Questions..-
If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.