This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Linked List Iterator return null nodes

 
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


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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.....
 
Sheriff
Posts: 21802
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Pay attention! Tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!