Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem With Linked List  RSS feed

 
Lionel Goodheart
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone. I have a problem with a linked list program I am currently working on. In the main body, a word is entered at a time. The word is then stored into a node and inserted alphabetically in its place. I am current getting all sorts of null pointer exceptions, so I was wondering if anyone could take a look at this and see what it is that I am doing wrong (btw - this needs to be done using a linked list).
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-If your head node isn't a dummy node, remember to check and make sure your new node isn't supposed to be the new head.
-Please consider renaming "word" to something like "newNode" because "word" sounds like a String, not a ListNode.
Try making your logic plainer by creating a couple booleans named such that it is clear what you are testing.
You might re-name your ListNodes x and y something like currentNode and nextNode, then drawn a picture of what happens as the method executes.
What you want to do is

[ March 12, 2004: Message edited by: Elouise Kivineva ]
 
Lionel Goodheart
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I worked out the method, and it work fine except for one reason. The second word that is entered ALWAYS ends up going to the end of the list. (For those unfamiliar with the program, read the first paragraph of the first post). Anyone know what could possibly be wrong with this?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!