Hello.
I am having a problem with this certain code. Every call with this method (listI()) will return a node. After the list reaches null, it will go back to the start. On the first call on the tester, there is no problem. But when the method is called for the second time, it does not stop. What's wrong with the code? What are the possible remedies I can do for this code?

Node listI(){
		Node e = null;
		Node idx = start;
		int c = 0;
		
		while(idx!=null){
			c++;
			idx = idx.next;
		}
		
		idx = start;
		
		if(c == count){ //count is a global variable. 
				idx = idx.next;
				e = idx;
				if(idx == null){
					e = null;
					idx = start;
				}
		}else{
			count = c;
			idx = idx.next;
			e = idx;
			if(idx == null){
				e = null;
				idx = start;
			}
		}
		
		if(e == null){
			System.out.println("End of list");
		}

		return e;
	}

Edited 6 Years Ago by chixm8_49: n/a

Well, first I can say that doing e = null after having done e= idx is redundant. If idx is null at that point so is e.

Another thing I don't understand is why you only ever return the first item. Is that what you want?

Also, if the method "does not stop", I can only assume you have a loop somewhere in your list I.E. (0 is start)
0 -> 1
1 -> 2
2 -> 3
3 -> 1

Well, first I can say that doing e = null after having done e= idx is redundant. If idx is null at that point so is e.
-->Sorry about that. I did not notice it right away. :P

Another thing I don't understand is why you only ever return the first item. Is that what you want?
-->The use of this method is that every call of this method will return a node. Once it reaches the last node, it will return to the start. It's like:
start -> node -> last node -> null -> start -> node ... (the cycle repeats)

If the tester is intended to print every node, it is like:

Node n;
n.listI(); // prints the contents of the start
n.listI(); // prints the contents of start.next
n.listI(); // prints the contents of last node
n.listI(); // null

Also, if the method "does not stop", I can only assume you have a loop somewhere in your list I.E. (0 is start)
0 -> 1
1 -> 2
2 -> 3
3 -> 1

--> I also assume that this is what is happening though I do not know how to solve it. :(

If your "start" Node is null, then your method will never run like you want it to. Try checking to see if start is null simply by doing this:

if (start == null) System.out.println("start node null error");

Furthermore, as far as I can tell, this loop has no effect since you set idx to 'start' before and after the loop.

while(idx!=null){
c++;
idx = idx.next;
}

Edited 6 Years Ago by BestJewSinceJC: n/a

Since you just responded... Post your code and I'll help you right now.

Here's the code with its latest changes though still, there are a lot of errors:

Node listI(){
		Node e = null;
		Node idx = start;
		int c = 0;
		
		while(idx!=null){
			c++;
			idx = idx.next;
		}
		
		idx = start;
		
		if(c == count){
				idx = start.next;				
				while(idx!=null){
					idx = start.next;
				}
			
				e = new Node(idx);
			
				if(e == null){
					System.out.println("End of list");
				}
				
		}else{
			count = c;
			idx = start;
			e = new Node(idx);
					
		}
		

		return e;
	}

Check out my previous comment. I could be wrong (it has happened before :) ) - but either way, I'm going to get to sleep now. Once you check out what I said, reply back if you have any more issues.

This article has been dead for over six months. Start a new discussion instead.