wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> Implementing stack
(Message started by: tuxilogy on May 6th, 2006, 11:58pm)

Title: Implementing stack
Post by tuxilogy on May 6th, 2006, 11:58pm
How to implement a Stack by using two queues

Title: Re: Implementing stack
Post by SMQ on May 7th, 2006, 6:38am
Why am I starting to get the feeling we're doing your 2nd-year CS homework for you... ;)

Here's a hint: there are two simple implementations; depending on which one you choose, either push or pop (of the implemented stack) is going to be inefficient.

--SMQ

Title: Re: Implementing stack
Post by gaps on May 9th, 2006, 3:25am
Take the first queue , enqueue the element when the operation is Push.
When the operation is pop, dequeue the first queue and  enqueue all the elements in the second queue except for the last element in the first queue. Return this element as popped element. Swap the references of the 2 queues.



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board