943,833 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1011
  • Java RSS
Jan 21st, 2009
0

queue question

Expand Post »
When you put an item on a queue, how do you show the items inside the queue? do you use peek method? the situation is a linked queue...
Similar Threads
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Jan 21st, 2009
0

Re: queue question

When putting an item inside the queue why do you want to show the queue. Or is it some assignment requirement that on every addition of element to the queue you need to show the queue structure ?
If yes, then write some method that traverses the entire queue and shows the data stored ar every location of the list queue.
If not give us details.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Jan 21st, 2009
0

Re: queue question

Click to Expand / Collapse  Quote originally posted by verruckt24 ...
Or is it some assignment requirement that on every addition of element to the queue you need to show the queue structure ?.
Yes!..

Click to Expand / Collapse  Quote originally posted by verruckt24 ...
If yes, then write some method that traverses the entire queue and shows the data stored ar every location of the list queue.
.
Java Syntax (Toggle Plain Text)
  1. public String toString(){
  2. String s = "[ ";
  3. if (front.info != null){
  4. s += "( " + peek().toString() + " )" + " ]";
  5. }else{
  6. s += "]";
  7. }
  8. return s;
  9. }

my prof. instructed me or siad to have an tostring method for that.. so i had the above. but it shows only the first element added to the queue.. what's wrong with it?
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Jan 21st, 2009
0

Re: queue question

You need to traverse the queue entirely for that, currently you are just showing the info of the first node.
Do something like this :
java Syntax (Toggle Plain Text)
  1. public void showQueue(myQueue head){
  2. myQueue temp = head;
  3. while(temp != null){
  4. System.out.println(temp.data.toString()); // prints out data at this node.
  5. temp = temp.next; // next should be the link to the next node
  6. }
  7. }

Don't assume my code to be working, rather the opposite, just treat it as a pseudocode for the traversing and printing logic.
Last edited by verruckt24; Jan 21st, 2009 at 7:57 am.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Jan 21st, 2009
0

Re: queue question

Thanks!..
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Writing to a file
Next Thread in Java Forum Timeline: Specification for data transfer in j2me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC