Home » Data Structure & Algorithms » Design a queue using stacks

Design a queue using stacks


Question :

WAP to implement a queue(FIFO) using stacks(LIFO).

If you are not aware of what is queue ?Please see the Queue tutorial.

Algorithm :

Iterative Solution :

Declare two stacks stack1 and stack2 .

enQueue(Object obj)
               Step 1 : Push obj  to stack1

               Step 1 : If both stacks are empty then return -1.
               Step 2 : If stack2 is empty ,While stack1 is not empty, push everything from stack1 to stack2.
               Step 3 : Pop the element from stack2 and return it.

Program :

OutPut :


Leave a comment

Your email address will not be published. Required fields are marked *

seven + 15 =