aspose file tools*
The moose likes Beginning Java and the fly likes oldskool linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "oldskool linked list " Watch "oldskool linked list " New topic
Author

oldskool linked list

f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87


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 ]

Imagination is more important than knowledge "Albert Einstein"
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

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.


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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
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
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: 39408
    
  28
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
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
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

Joined: Feb 06, 2008
Posts: 4
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

Joined: Mar 02, 2008
Posts: 87


i took out the printList() method as well as placing the assignment to newlink.link to where you specified. no dice ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
. . . and welcome to the Ranch, S Keith
S Keith
Greenhorn

Joined: Feb 06, 2008
Posts: 4
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

Joined: Mar 02, 2008
Posts: 87
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: oldskool linked list