How To Use Linked Lists in Java
How To Use Linked Lists in Java
• Doubly linked lists (DLLs) are an extension of basic linked lists, but
they contain a pointer to the next node as well as the previous node.
This ensures that the list can be traversed in both directions. A DLL
node has three fundamental members:
• The data
• Pointer to the next node
• Pointer to the previous node
Circular linked list
• Circular linked lists function circularly: the first element points to the
last element, and the last element points to the first. A single linked list
and double linked list can be made into a circular linked list. The most
important operations for a circular linked list are:
Sample dance in PE
• Class Linked list
• As mentioned above, the Singly Linked list is made up of nodes that are
linked together like a chain. Now to access this chain, we would need a
pointer that keeps track of the first element of the list.
• As long as we have information about the first element, we can traverse the
rest of the list without worrying about memorizing their storage locations.
• The Singly Linked List contains a head node: a pointer pointing to the first
element of the list. Whenever we want to traverse the list, we can do so by
using this head node.
• Below is a basic structure of the Singly Linked List’s class:
How to create and use a Linked List
• Linked lists are fairly easy to use since they follow a linear structure.
They are quite similar to arrays, but linked lists are not as static, since
each element is its own object. Here is the declaration for Java Linked
List class:
• Let’s see a more detailed example of that in code. Here is how we
create a basic linked list in Java:
Adding Elements to a Linked List