Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implement linked list in java

 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic