How do you implement "Queue" functionality by using "2 Stacks"

Editorial / Best Answer

saradalincoln  

  • Member Since Jun-2007 | Jul 17th, 2007


hi,

1. Take 2 empty stacks.
2. Fill the 1st stack with some items (eg: input 100, 101, 102, 103)
3. Now read the items from 1st stack in LIFO order and write to 2nd stack.
    (By this the 2nd stack will get the items in the order .. 103, 102, 101, 100)
4. Now read the items from 2nd stack in LIFO order i.e. the output will be
    100, 101, 102, 103. 
    (this is the queue order FIFO with respect to the actual items entered in 1st
     stack.)


Thanks,
Sarada.

Showing Answers 1 - 1 of 1 Answers

hi,

1. Take 2 empty stacks.
2. Fill the 1st stack with some items (eg: input 100, 101, 102, 103)
3. Now read the items from 1st stack in LIFO order and write to 2nd stack.
    (By this the 2nd stack will get the items in the order .. 103, 102, 101, 100)
4. Now read the items from 2nd stack in LIFO order i.e. the output will be
    100, 101, 102, 103. 
    (this is the queue order FIFO with respect to the actual items entered in 1st
     stack.)


Thanks,
Sarada.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions