It's not a secret anymore!
The moose likes Java in General and the fly likes Implement linked list in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Implement linked list in java" Watch "Implement linked list in java" New topic

Implement linked list in java

Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70

Can anyone tell me how to implement linked list in java without using JAVA API's.

Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
hi Nilesh! you want the entire code?? i am sure you dont want the entire code, only a starting point, right?

since you know linked lists, i will not talk on that. the key concept while implementing linked lists in Java is that objects are always passed by reference.

here is a sample implementation. this is bare bones implementation, but i think sufficient to get you started.

Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Neeraj outlined the basic idea, but here are a couple of details you might keep in mind.

1) Java's garbage collection features make removing a Node from a linked list easier, as you don't have to explicitly set a pointer to a Node to null.

2) When implementing a remove(Object key) method for your own linked list, you'll start to appreciate why Java included Iterator interfaces. You might want to implement your own iterator() function in your linked list that returns a special implementation of Iterator to remove Nodes in your linked list. That will make your code more readable and more OO.
I agree. Here's the link:
subject: Implement linked list in java
It's not a secret anymore!