Author
Implementing a stack using 2 queues (Read 1225 times)

Post: 15
Implementing a stack using 2 queues
on: Thursday 12 November, 2009, 03:05:29 PM

Can you help me out , I need to implement a stack using 2 queues

No Image
Post: 46
Re: Implementing a stack using 2 queues
on: Friday 13 November, 2009, 11:59:52 AM

Let the 2 queues be queue1,queue2
Suppose we enter 1 2 3 4 - the expected result from stack is 4321
  • Put the first number in queue1 i.e. 1
  • Now when 2 comes pop 1 from queue1 and push it in queue2
  • Insert 2 back in queue 1
  • So now if we pop the data we will get 2 and then 1
  • Similarly Now if three comes
  • Pop 1 and 2 from queue1 and push it in queue2
  • Push 3 in queue1
  • Now again pop 1 and 2 from queue2 and push it in queue1
In this way follow it follow it for rest of the numbers