Whats in this post?
- Output (not available)
- Background and Explanation of the code
To copy code: Click “View Source” on the right of this code snippet.
Background and Explanation of the code
Linked Lists are a very common way of storing arrays of data. The major benefit of linked lists is that you do not specify a fixed size for your list. The more elements you add to the chain, the bigger the chain gets.
There is more than one type of a linked list, although for the purpose of this tutorial, we’ll stick to singly linked lists (the simplest one). If for example you want a doubly linked list instead, very few simple modifications will give you what you’re looking for. Many data structures (e.g. Stacks, Queues, Binary Trees) are often implemented using the concept of linked lists.
Above code of the “LinkedList” class follows. It is a commented oversimplification of the “LinkedList” class that’s already built into Java. Please note that indexes start at 1 and not 0 (like in the Java “LinkedList” class), so the first element in the list has index 1. One just need to create Object of “LinkedList” class and he/she can use it easily. For more functionality, one can add more methods in “LinkedList” class.