Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to build a queue from 2 stacks??

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic