Java

How to Use Class to Implement Linked List in Java?

A linked list is a data structure in Java that is made up of several nodes, each of which has a data element and a reference to the node after it. The first and last nodes are represented as the head and tail of a list. The benefit of a linked list is that it provides quick addition and removal of list elements, especially in comparison to arrays.

This guide will demonstrate the practical implementation of a linked list in Java.

How to Use Class to Implement Linked List in Java?

A linked list can be applied utilizing a class that defines the nodes of the list. Each node of the list has a reference to the upcoming node in the list, and the data element is stored.

To implement a linked list, first, create a constructor that takes the data as a parameter and initializes the next node reference to null in the “Node” class. After that, create setter and getter methods for the data and next node fields.

After that, create a constructor that initializes the first node reference to null in the “LinkedList” class. Then, create methods to insert a node to the start or end of the list, remove a node from the start or end of the list, and print the list.

Example
Here is an example implementation of a simple linked list in Java using a class:

public class LinkedList {
   
    private Node head; // Reference to the head node
   
    // Private inner class representing a node in the linked list
    private class Node {
        int data;
        Node next;
       
        // Constructor
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
   
    // Method to add a new node to the end of the list
    public void add(int data) {
        Node newNode = new Node(data);
        if (head == null) {    // Apply condition
            head = newNode;
        } else {
            Node current = head;  
            while (current.next != null) {  // Apply condition
                current = current.next;
            }
            current.next = newNode;
        }
    }
   
    // Method to print all the elements of the list
    public void printList() {
        Node current = head;
        while (current != null) {  // Apply condition
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
}

The explanation of the code is mentioned below:

  • First, define a “LinkedList” class that contains a reference to the head node of the linked list.
  • After that, define a private inner class “Node” that denotes a single node in the list.
  • Furthermore, the “add()” method adds a new node to the end of the list by creating a new node with the existing data and appending it to the end of the list.
  • In the end, the “printList()” method prints all the elements of the linked list by iterating through the list starting from the head node and printing the data element of each node.

To use this implementation, create a new instance of the class and add some elements such as 1, 2, and 3, like this:

public static void main(String[] args) { // main function
    LinkedList list = new LinkedList();
    list.add(1);
    list.add(2);
    list.add(3);
    list.printList();
}

This creates a new LinkedList object, adds three elements to the list (1, 2, and 3), and then prints all the elements of the list.

Output

The output shows that “1 2 3” has printed all the elements of the list in the terminal.

Conclusion

To implement a linked list in Java utilizing a class, first, create a “Node” class that holds a reference to the upcoming node and a data field. Next, create a “LinkedList” class that contains a reference to the first node and provides methods for adding and removing nodes. Users are allowed to evaluate the implementation by creating an instance of the Linked List class and performing distinct functions.

This article has explained the procedure to implement a linked list in Java using the class.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.