Simulating a Stack using Queues

can anyone help me with the concept of simulating a stack using 2 queues? I'm not quite sure the theory behind this or how to begin implementing it. thanks for the help!
[177 byte] By [johnsmith954a] at [2007-11-26 19:22:47]
# 1

You store everything in one queue, until you need a value from the "stack". Then, you take the first element of the queue and places it on the second queue (which is currently empty). Keep doing this until there is only one element left on the original queue, that was the last element to be placed on the queue, and so is the element you want to receive from your "stack".

abu5ea at 2007-7-9 21:43:45 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks a lot! ill start implementing and ill get back to you if i have any questions.On a side note, how would you sort a queue using a stack?again, thanks for the help!
johnsmith954a at 2007-7-9 21:43:45 > top of Java-index,Java Essentials,Java Programming...
# 3

I'm guessing this is coursework?

It's half 5 in the morning so I'm not willing to put too much thought into this, but I think it would be something like:

You could take the head of the queue, and do:

IF the head is greater than the top of the stack (or whichever way around you want to sort it), place it on top of the stack, otherwise put it to the tail of the queue, and compare the next head.

You would then need to do something to prevent the queue from just being circulated over and over if the top of the stack was greater than everything in that, and in that case you would remove the top of the stack and put it at the back of the queue, and repeat the same process.

This will need a bit more thought (ie. what if the two at the top of the stack are BOTH greater than anything in the queue), but I think this is the right idea.

Coding it and trial and error will probably help you to solve this one, good luck.

abu5ea at 2007-7-9 21:43:45 > top of Java-index,Java Essentials,Java Programming...
# 4
http://forum.java.sun.com/thread.jspa?threadID=5140172&tstart=0DON'T DOUBLE POST!It's rude and it wastes peoples time. I wasted my time providing answers in your other thread while you are getting answers from abu5e here.DON'T DO IT AGAIN!
floundera at 2007-7-9 21:43:45 > top of Java-index,Java Essentials,Java Programming...