Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Liked List help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Liked List help" Watch "Liked List help" New topic
Author

Liked List help

Leonardo Pjetri
Greenhorn

Joined: Dec 21, 2004
Posts: 29
I�m learning java at the moment and I�m having problems with the method below:
Well what it dose is inserts a link in a linked list, what im trying to do is sort insert by the string data in the link.
And to compare tow strings I m using the compareTo method but when I use it as seen below it throws a null pointer exception if the liked List is empty.

But if I take this out > int value = ids.compareTo(first.dData); it will work fine it just won�t be sorting anything.

In other words it only works if the list is not empty with the comareTo ( ) method, which is wired.

public void insert(int key,String ids)
{

Link newLink = new Link(key,ids);
Link previous = null;
Link current = first;

int value = ids.compareTo(first.dData);

while(current != null ){
previous = current;
current = current.next;
}
if( previous==null)
first = newLink;
else
previous.next = newLink;
newLink.next = current;
}
Any help will help
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The problem is that you're testing for an empty list after you've assumed it wasn't empty.To solve this you need to handle the empty-list case up front -- no looping, no compareTo (nothing to compare!), just append the new link node and return from the method.

Once you tackle that, you can work on getting the other corner cases handled. Good luck!
Leonardo Pjetri
Greenhorn

Joined: Dec 21, 2004
Posts: 29
thanx this is what i ended up with

public void insert(int key,String ids){

Link newLink = new Link(key,ids);
Link previous = null;
Link current = first;

//If list is empty, put element at front and exit
if ( first == null ){
first = newLink;
return;
}



while( current != null ){
if ( ids.compareTo(current.dData) <= 0 ) break;

previous = current;
current = current.next;
}

if ( current == null )
//Insertion point at end
previous.next = newLink;
else if ( previous == null )
//Insertion point at front
first = newLink;
else
previous.next = newLink;

newLink.next = current;
}
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
That looks good, though you can still simplify the end a bit.Simplifying all the way leaves you withYour code should work as-is, but I wanted to get you thinking more rigorously about handling references like this in data structures. It's very easy to get the logic twisted.
[ December 22, 2004: Message edited by: David Harkness ]
Leonardo Pjetri
Greenhorn

Joined: Dec 21, 2004
Posts: 29
Man this is doing my head in, and I thought I was good at this and I have to go on and write a queue. I don�t think I can simplify it as u suggest see below:

public void insert(int key,String ids){

Link newLink = new Link(key,ids);
Link previous = null; // previous set to null here
Link current = first;

//If list is empty, put element at front and exit
if ( first == null ){
first = newLink;
return;
}
//start while loop
//current will be first and previous null at start
while( current != null ){
if ( ids.compareTo(current.dData) <= 0 ) break; //stop while loop if condition met
previous = current;
// previous will still be null if condition above true first time round
// this will not take place
current = current.next;
}
if ( current == null )
previous.next = newLink;
else if ( previous == null )
first = newLink;
else
previous.next = newLink;
newLink.next = current;
}
What do u think?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
My bad! I was still looking at the original while loop without the if test. You are correct that you have to handle the case where you prepend the new link at the start of the list. Well caught; I'd say you are pretty good at it!

For these things what always helps me is drawing pictures. I was helping a friend of mine with exactly this sort of thing and kept nisistnig (over the phone) that he draw pictures. It took much prodding, but upon finally drawing his first picture, his only reply was, "Why didn't you insist more strongly?"

My only recommendation then, given that it works, is to clarify the results of if tests at the end, and this is more style than correctness. It's more so that you'd understand what you were doing if you looked at this in a month.I've definitely become a fan of always using { and }. If you leave them off but later add a second statement, you're likely to forget to add them. And unlike Python, Java will ignore that the second statement is indented, introducing a runtime error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Liked List help
 
Similar Threads
Linked List Insertion and Deletion help
LinkedList
Insert into Doubly LinkedList
oldskool linked list
Linked List in Java