Hey everyone,

I'm new to the concept of stacks and queues. I think I have the basics, but I have a quick question.

If I am wanting to swap an object, say x, for another object, say y, in a stack or a queue, is this possible without introducing an array to store the objects? Am I missing someting fundemental about the ADTs?

Thank you!

In a queue you can cycle it around by taking the element at the head of the queue and moving it to the end. Doing this repeatedly will give you access to each element of the queue in turn without requiring any additional storage.

In a stack you need to pop off the upper elements to get access to the lower elements, so if you want to do anything with the lower elements you will need to store the upper elements somewhere until it is time to push them back on. A second stack seems ideal for that purpose.

On the other hand, if you use a java.util.Stack then you can access any element at any time and replace it freely because java.util.Stack is really a java.util.Vector. I don't recommend using Stack. You should prefer java.util.Deque.

commented: bguild.incrementReputation(); +13