• Post Reply Bookmark Topic Watch Topic
  • New Topic

Linked List Iterator return null nodes  RSS feed

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to find out a way to resolve his issue. My code doesn't compile an error but returns null for each node of the linked list.

Here is where I think my problem lies.....



Here is my class with two inner classes.....I took out a lot of the methods to make it a lot less code


 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your push method, when you push the first Object into the list, you create a new node, assign its nextNode to null, and assign it to head. You never change this, so head.nextNode will remain null. Then in your iterator, you always return a reference to the same Object, head.nextNode, never looking at the first nod (head) or trying to advance past the second node (head.nextNode).

So the first thing you need to do is modify your push method so it creates a link between the first node (head) and the second node (via head.nextNode).

Then you need to get rid of your iterator method (and entire implementation) and write out on paper exactly what you expect it to do. Do it in your native language first (not Java), then turn them into simplistic instructions, drawing diagrams so you can clearly illustrate (as if to a child) what you want the illustrator to do. Then once you have the description and details instructions, translate them to code.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:In your push method, when you push the first Object into the list, you create a new node, assign its nextNode to null, and assign it to head. You never change this, so head.nextNode will remain null. Then in your iterator, you always return a reference to the same Object, head.nextNode, never looking at the first nod (head) or trying to advance past the second node (head.nextNode).

So the first thing you need to do is modify your push method so it creates a link between the first node (head) and the second node (via head.nextNode).

Then you need to get rid of your iterator method (and entire implementation) and write out on paper exactly what you expect it to do. Do it in your native language first (not Java), then turn them into simplistic instructions, drawing diagrams so you can clearly illustrate (as if to a child) what you want the illustrator to do. Then once you have the description and details instructions, translate them to code.


The problem was head.nextNode....it should have been node.nextNode......

+1 for diagrams.....
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this some school assignment? If not, why not use java.util.LinkedList? You shouldn't reinvent the wheel unless you're being told to do so for educational purposes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!