Q:

Java program to create a Circular Linked List of N nodes and count the number of nodes

belongs to collection: Java Circular Linked List Programs

0

In this program, we have to find out the number of nodes present in the circular linked list. We first create the circular linked list, then traverse through the list and increment variable 'count' by 1.

Algorithm

  • Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  • Define another class for creating the circular linked list and it has two nodes: head and tail. It has two methods: add() and display() .
  • add() will add the node to the list:
    • It first checks whether size is null or head is null; then it will insert the node as the head.
    • Both head and tail will point to a newly added node.
    • If the head is not null, the new node will be the new tail, and new tail will point to the head as it is a circular linked list.

a. countNodes() will count the number of nodes present in the list.

  • Define new node current which will point to the head node.
  • Traverse through the list to count the nodes by making the current node to point to next node in the list till current points to head again.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

public class CountNodes {    
        //Represents the node of list.    
        public class Node{    
            int data;    
            Node next;    
            public Node(int data) {    
                this.data = data;    
            }    
        }    
            
        public int count;    
        //Declaring head and tail pointer as null.    
        public Node head = null;    
        public Node tail = null;    
            
        //This function will add the new node at the end of the list.    
        public void add(int data){    
            //Create new node    
            Node newNode = new Node(data);    
            //Checks if the list is empty.    
            if(head == null) {    
                 //If list is empty, both head and tail would point to new node.    
                head = newNode;    
                tail = newNode;    
                newNode.next = head;    
            }    
            else {    
                //tail will point to new node.    
                tail.next = newNode;    
                //New node will become new tail.    
                tail = newNode;    
                //Since, it is circular linked list tail will point to head.    
                tail.next = head;    
            }    
        }    
            
        //This function will count the nodes of circular linked list    
        public void countNodes() {    
            Node current = head;    
            do{    
                //Increment the count variable by 1 for each node    
                count++;    
                current = current.next;    
            }while(current != head);    
            System.out.println("Count of nodes present in circular linked list: "+count);    
        }    
            
        public static void main(String[] args) {    
            CountNodes cl = new CountNodes();    
            cl.add(1);    
            cl.add(2);    
            cl.add(4);    
            cl.add(1);    
            cl.add(2);    
            cl.add(3);    
            //Counts the number of nodes present in the list    
            cl.countNodes();    
        }    
}    

Output:

Nodes of generated doubly linked list: 
4 2 5 1 6 3 7 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to create a Circular Linked List of n... >>
<< Java program to create and display a Circular Link...