This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Implement linked list in java" Watch "Implement linked list in java" New topic
Author

Implement linked list in java

Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Hi,

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

Regards
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Implement linked list in java