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...

Recommended Answers

All 4 Replies

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.

Or is it some assignment requirement that on every addition of element to the queue you need to show the queue structure ?.

Yes!.. :)

If yes, then write some method that traverses the entire queue and shows the data stored ar every location of the list queue.
.

public String toString(){
    	String s = "[ ";
    	if (front.info != null){
    		s += "( " + peek().toString() + " )" + " ]";
    	}else{
    		s += "]";
    	}
    	return s;
    }

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?

You need to traverse the queue entirely for that, currently you are just showing the info of the first node.
Do something like this :

public void showQueue(myQueue head){
    myQueue temp = head;
    while(temp != null){
        System.out.println(temp.data.toString()); // prints out data at this node.
        temp = temp.next; // next should be the link to the next node
    }
}

Don't assume my code to be working, rather the opposite, just treat it as a pseudocode for the traversing and printing logic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.