• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Linked List Iterator return null nodes

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic