We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Stacks and Queues question

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!

2
Contributors
1
Reply
15 Minutes
Discussion Span
3 Months Ago
Last Updated
2
Views
fatalaccidents
Light Poster
43 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

bguild
Posting Whiz
333 posts since Oct 2012
Reputation Points: 167
Solved Threads: 74
Skill Endorsements: 7

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0541 seconds using 2.69MB