This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes how to build a queue from 2 stacks?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to build a queue from 2 stacks??" Watch "how to build a queue from 2 stacks??" New topic
Author

how to build a queue from 2 stacks??

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i know that in order to flip the hole order of the objects then
we put the into one stack and that pop each one
and push it into the second one


but when i tried to build it
i wanted to copy that proccess so i
pushed to st1 poped from st1
and then pushed into st2.
didnt work at all
[ February 03, 2008: Message edited by: donaldth smithts ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

but when i tried to build it
i wanted to copy that proccess so i
pushed to st1 poped from st1
and then pushed into st2.
didnt work at all


I think you misinterpreted your instructor's instructions. To add to the queue, you simpy push onto stack 1. To remove from the queue, you simply remove from stack 2. Only when stack 2 is empty, and you need to remove, do you pop from stack 1 and push to stack 2 -- and in that case, you must completely move everything from stack 1 to stack 2.

Regardless, show us your code. We can't figure out where it "didnt work at all" without an idea of what you are doing.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In fact I think the best way to get this done is execute the algorithm by hand, for example with a stack of index cards. Only when you got that working, that is you understand how the algorithm works, start coding it in Java.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
jQuery in Action, 2nd edition
 
subject: how to build a queue from 2 stacks??
 
Similar Threads
Stack
Stack referencing stack
tokenizer help
About string again
exception related