• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Why the nodes inserted are not getting registered?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!

I am learning Singly Linked List implementation in Java. So in the below code whenever I am inserting a node and trying to display the nodes, none of the nodes are getting printed. I checked that my head node is always remaining null. I can't understand why. Maybe I am making a silly mistake somewhere but really can't figure it out. Any help will be appreciated.

Thank you.

 
Eric Kaiser
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solved the issue. Pretty silly one.
 
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done

But what was the problem?
 
Eric Kaiser
Greenhorn
Posts: 15
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well done

But what was the problem?



You see in the main function I have taken a do-while loop. In that do-while loop I am creating an object of type LinkedList every time the loop gets executed. So the head node in LinkedList class is getting created for each object of LinkedList type, though the reference variable ll remains the same.

Now I can either take this line out of the loop or can make the head node static. That did the trick
 
Campbell Ritchie
Marshal
Posts: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I can see the problem now; thank you for explaining it. You are lucky that you are unlikely to repeat that mistake.
 
Eric Kaiser
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, I can see the problem now; thank you for explaining it. You are lucky that you are unlikely to repeat that mistake.



You are welcome . Yeah for sure. I thank HeadFirst Java for making these basic concepts so clear.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic