Java Program to Create a Singly Linked List, Add Elements to it and Display

In this article, we are going to see how we can create a singly linked list, add elements to it and display it by using Java programming language.

Java Program to Create a Singly Linked List, Add Elements to it and Display

Singly LinkedList:

In Java singly linkedlist is a data structure which contains a series of elements. Each element in the list contains the element and a pointer to next element which is called a node. Where the first node is termed as head and last node is termed as tail of the linkedlist. Last node pointer contains null.

Approach:

  • Create a class node to contain the data and the link of all nodes.
  • Initialize the head and tail as null as the list is initially empty.
  • Create a method add() which adds the element to the node.
  • Create another method that checks if the linked list is empty else displays the elements.
  • Now from the main function create an object of the class which creates an empty linked list for us.
  • Now add some elements to the list using the add() method.
  • Finally, call the show() method to display the linked list.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and tail of the linkedlist as NULL  
    public Node head = null;    
    public Node tail = null;  
    // method to add a node to the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        //if the list is empty then head and tail both will point to newNode
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
        System.out.println(data+" has been added to the list.");
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        //Node curr refers to the head element
        Node curr = head;
        System.out.println("Trying to display the linked list...");
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        // display the nodes
        ll.show();
    }
}
Output:

10 has been added to the list.
20 has been added to the list.
30 has been added to the list.
40 has been added to the list.
50 has been added to the list.
Trying to display the linked list...
The nodes are:
10,20,30,40,50,

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Leave a Comment