Google News
logo
Java program to create a singly linked list of n nodes and count the number of nodes
In the following example of Java program to create a singly linked list of n nodes and count the number of nodes :
Program :
import java.util.Scanner;

public class SinglyLinkedList {
    
    // Node class
    static class Node {
        int data;
        Node next;
        
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    
    // Head of the linked list
    Node head;
    
    // Method to add a node to the end of the list
    public void add(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }
    
    // Method to count the number of nodes in the list
    public int count() {
        int count = 0;
        Node current = head;
        while (current != null) {
            count++;
            current = current.next;
        }
        return count;
    }
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        SinglyLinkedList list = new SinglyLinkedList();
        System.out.print("Enter the number of nodes: ");
        int n = input.nextInt();
        for (int i = 1; i <= n; i++) {
            System.out.print("Enter the data for node " + i + ": ");
            int data = input.nextInt();
            list.add(data);
        }
        System.out.println("The linked list is: ");
        list.display();
        System.out.println("\nThe number of nodes in the linked list is: " + list.count());
    }
    
    // Method to display the list
    public void display() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }
}
Output :
Enter the number of nodes: 5
Enter the data for node 1: 2
Enter the data for node 2: 7
Enter the data for node 3: 9
Enter the data for node 4: 12
Enter the data for node 5: 18
The linked list is: 
2 7 9 12 18 
The number of nodes in the linked list is: 5
The above program defines a Node class to represent a node in the linked list. Each node contains an integer data and a reference to the next node in the list. The SinglyLinkedList class contains a reference to the head of the list, methods to add nodes to the end of the list, count the number of nodes in the list, and display the list.

In the add method, a new node is created with the specified data, and added to the end of the list. If the list is empty, the new node becomes the head. If the list is not empty, the method traverses the list until it reaches the last node, and adds the new node as its next node.

In the count method, the list is traversed from the head node to the end, and a counter variable is incremented for each node in the list. The value of the counter is returned as the number of nodes in the list.

In the main method, the user is prompted to enter the number of nodes to create, and the data for each node is entered. Nodes are added to the list using the add method, and the list is displayed using the display method. The number of nodes in the list is counted using the count method, and the result is printed to the console.