To implement a stack using two queues

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 3
Reputation: preethis is an unknown quantity at this point 
Solved Threads: 0
preethis preethis is offline Offline
Newbie Poster

To implement a stack using two queues

 
1
  #1
Jun 21st, 2005
Hi,

How to implement a stack using two queues ? Help pls!

Regards,
Preethi
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: To implement a stack using two queues

 
0
  #2
Jun 21st, 2005
Think of it this way: For each new value that's pushed onto the 'stack', you pop all of the values from queue1 onto queue2, then push the new value onto queue1. Taking a simple example, say you have queue1 with a single value, 1, and you want to push 2 onto it so that the next pop gives you 2 instead of 1:

queue1: * 1
queue2: *

Empty queue1 onto queue2:

queue1: *
queue2: * 1

Push 2 onto queue 1:

queue1: * 2
queue2: * 1

Then empty queue2 onto queue1:

queue1: * 1 2
queue2: *

So now, by pushing 1 and then 2 onto the queue this way, popping them both off would give you 2 1, just like a stack, instead of 1 2, like a queue.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: preethis is an unknown quantity at this point 
Solved Threads: 0
preethis preethis is offline Offline
Newbie Poster

Re: To implement a stack using two queues

 
0
  #3
Jun 22nd, 2005
Hi,

Is there a mistake in the last step ?

The last step that is given is:
Then empty queue2 onto queue1:
queue1: * 1 2
queue2: *
I think the last step is :
queue1: * 2 1
queue2: *
Is it right ??
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: To implement a stack using two queues

 
0
  #4
Jun 22nd, 2005
> Is it right ??
No, you're thinking that the left side of my queues designate the front, which wasn't the intention. It works like this in the example that I gave:

New values go in here -> 1 2 -> Old values come out here

If you want to think of it the other way around, that's okay too, just reverse the values.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 17625 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC