oldskool linked list

the above code compiles... however i'm getting an infinite loop on the second while in the class file. please comment out the System.out's in the class file; they are for my debugging. my data file is the australian scrabble sowpods 4 letter list with each word on a single line. also i'm getting repeats in the list, which i added to the file but "i think" i shouldn't have in the outfile. i know it's a logic error on my part. any help at all will be greatly appreciated. thanks. [ March 23, 2008: Message edited by: f. nikita thomas ]

You are seeing the duplicates because the code currently doesn't check for them; it will still merrily add them to the linked list. You should add a check to avoid adding the link to the list when you discover there's already a similar item.

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James

Are you trying to create a LinkedList implementation? If so, why have you declared the nodes as static?

f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87

posted Mar 23, 2008 16:01:00

0

what i'm attempting to do is a sorted linked list. i declared the Node variables as static because i thought that this would ensure only one instance of them per class creation ... or am i mistaken?

n.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41599

31

posted Mar 23, 2008 16:17:00

0

A linked list works like this: node0--->node1--->node2--->node3--->etc

So each node has a link to the node following it. A lot of linked lists also have a link to the preceding node (called a doubly-linked list). Each of these links is different for each node, so they can't be static. A static field would be the same for each node.

f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87

posted Mar 23, 2008 16:27:00

0

geez, i'll get rid of the static !!! btw do you see where i put myself on the merry-go-round? i've been at it since this morning and i just don't see it.

hey henry, i put the printList() method in the loop to see if new Nodes were being created. the loop existed before i added it; taking it out will make no difference. the while "is" sorting the data - although it won't stop:

the System.out.() statement is the culprit! the other System.out.()'s can remain for testing. why it's causing the loop is still a subject for my investigation. like Henry explained the printList() call will cause a loop, (hence his cryptic comment ...). there are still duplicates with the data but i introduced them and now that i'm not so dizzy, i can handle that issue. thanks to all for your assistance. i'm trying to be a "reaL" programmer, so i need the feedback. take care.