• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 22783
131
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.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic