Author |
Topic: Implementing stack (Read 562 times) |
|
tuxilogy
Newbie
Posts: 45
|
|
Implementing stack
« on: May 6th, 2006, 11:58pm » |
Quote Modify
|
How to implement a Stack by using two queues
|
|
IP Logged |
|
|
|
SMQ
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 2084
|
|
Re: Implementing stack
« Reply #1 on: May 7th, 2006, 6:38am » |
Quote Modify
|
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
|
|
IP Logged |
--SMQ
|
|
|
gaps
Newbie
Gender:
Posts: 1
|
|
Re: Implementing stack
« Reply #2 on: May 9th, 2006, 3:25am » |
Quote Modify
|
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.
|
|
IP Logged |
|
|
|
|