Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

oldskool linked list

 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


and



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 ]
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikita,

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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to create a LinkedList implementation? If so, why have you declared the nodes as static?
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

n.
 
Henry Wong
author
Marshal
Pie
Posts: 21218
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by f. nikita thomas:
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.

n.


Your Merry-Go-Round was created here (well, one of them anyway)....



Basically, you printList() method changed the value of newLink, to point to the last member of the list. So, the next line, chained it to a member within the list -- creating a circle.

Henry
[ March 23, 2008: Message edited by: Henry Wong ]
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

<snip>

Here
Here
Here
Here
Again
ABBS ListT$Node@1386000
ACHY ListT$Node@30e280
ACRE ListT$Node@a0dcd9
AEON ListT$Node@ecd7e
AGHA ListT$Node@6e1408
AIRY ListT$Node@a1807c
ANNA ListT$Node@147c5fc
ANOA ListT$Node@1d8957f
AREG ListT$Node@16a9d42
ASKS ListT$Node@cdedfd
BACK ListT$Node@c5c3ac
BACS ListT$Node@1ef9f1d
BAMS ListT$Node@12558d6
BAST ListT$Node@1c39a2d
BATE ListT$Node@1abab88
BATS ListT$Node@15a3d6b
BAWN ListT$Node@18a7efd
BEAD ListT$Node@b1c260
BEER ListT$Node@e7b241
BEYS ListT$Node@7c6768
BIOS ListT$Node@cf2c80
BISE ListT$Node@186db54
BLED ListT$Node@116471f
BOLT ListT$Node@167d940

ad infinitum ...


n.
[ March 23, 2008: Message edited by: f. nikita thomas ]
 
S Keith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it with the file: foo, bar, baz

You have a call in making it appear that you have duplicates.

The call to will also stop you properly linking the list because the reference is tampered with, before the link field in is set.

if you move above the call, then that would fix the problem, and you will see that the whole list is printed twice.



if you take out:



then you will get your list printed ok

[ March 23, 2008: Message edited by: S Keith ]
[ March 23, 2008: Message edited by: S Keith ]
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i took out the printList() method as well as placing the assignment to newlink.link to where you specified. no dice ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch, S Keith
 
S Keith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
howdy.

Can you give me your input file?

If you test it with



there is no problem.


This is exactly what i have in the file run0.java


and i get the output:


[ March 24, 2008: Message edited by: S Keith ]
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem solved thanks to S Keith. apparently:



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.

n.
[ March 24, 2008: Message edited by: f. nikita thomas ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic