0% found this document useful (0 votes)
24 views23 pages

How To Use Linked Lists in Java

This document discusses linked lists in Java. It describes linked lists as a data structure made up of nodes that point to the next node in the chain. It explains the different types of linked lists including singly linked lists, doubly linked lists, and circular linked lists. It provides code examples for creating a linked list class and adding elements to a linked list.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
24 views23 pages

How To Use Linked Lists in Java

This document discusses linked lists in Java. It describes linked lists as a data structure made up of nodes that point to the next node in the chain. It explains the different types of linked lists including singly linked lists, doubly linked lists, and circular linked lists. It provides code examples for creating a linked list class and adding elements to a linked list.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 23

How to use linked lists in Java

What is a Linked List?

• A linked list is a common data structure that is made of a chain of


nodes. Each node contains a value and a pointer to the next node in
the chain.
• The head pointer points to the first node, and the last element of the
list points to null. When the list is empty, the head pointer points to
null.
• Linked lists can dynamically increase in size. It is easy to insert and
delete from a linked list because unlike arrays, as we only need to
change the pointers of the previous element and the next element to
insert or delete an element.
• Some important applications of Linked Lists include:
• Implementing HashMaps, File System and Adjacency Lists
• Dynamic memory allocation: use linked lists of free blocks
• Performing arithmetic operations on long integers
• Maintaining a directory of names
Types of linked lists
• Since a linked list is a linear data structure, meaning that the elements
are not stored at contiguous locations, it’s necessary to have different
types of linked lists to access and modify our elements accordingly.
• There are a three different types of linked lists that serve different
purposes for organizing our code.
• Let’s take a look.
Singly linked list (Uni-directional)

• A singly linked list is unidirectional, meaning that it can be traversed


in only one direction from head to the last node (tail). Some common
operations for singly linked lists are:
Doubly linked list (Bi-directional)

• 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

You might also like