Signup/Sign In

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

In this tutorial, we will learn about the algorithm and its implementation for finding the number of nodes in a circular linked list in Python.

What is Circular Linked List?

Circular Linked List is a little more complicated linked data structure. In the circular linked list, we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements point to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required.

Here, 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.

As the output, we are required to get the count of nodes in the circular linked list.

Look at the given example to understand the working with input and output.

Input:

circular linked list : 3 5 1 2 6

Output:

5

Input:

circular linked list : 73 5 63 9 2 10 3

Output:

7

Algorithm

Step1: Define a Node class that represents a node in the list. It has two properties data and next which will point to the next node.

Step2: Define another class for creating the circular linked list and it has two nodes like head and tail. It has two methods: add() and display() .

Step3:

  1. add() will add the node to the list:
  2. It first checks whether the size is null or the head is null; then it will insert the node as the head.
  3. Both head and tail will point to a newly added node.
  4. If the head is not null, the new node will be the new tail, and the new tail will point to the head as it is a circular linked list.

Step4:

  1. countNodes() will count the number of nodes present in the list.
  2. Define new node current which will point to the head node.
  3. Traverse through the list to count the nodes by making the current node point to the next node in the list till the current points head again.

Python Program to Create a Circular Linked List of N nodes and count the number of nodes

#Represents the node of the list.  
class Node:  
    def __init__(self,data):  
        self.data = data;  
        self.next = None;  
   
class CreateList:  
    #Declaring head and tail pointer as null.  
    def __init__(self):  
        self.count = 0;  
        self.head = Node(None);  
        self.tail = Node(None);  
        self.head.next = self.tail;  
        self.tail.next = self.head;  
      
    #This function will add the new node at the end of the list.  
    def add(self,data):  
        newNode = Node(data);  
        #Checks if the list is empty.  
        if self.head.data is None:  
            #If list is empty, both head and tail would point to new node.  
            self.head = newNode;  
            self.tail = newNode;  
            newNode.next = self.head;  
        else:  
            #tail will point to new node.  
            self.tail.next = newNode;  
            #New node will become new tail.  
            self.tail = newNode;  
            #Since, it is circular linked list tail will point to head.  
            self.tail.next = self.head;  
              
    #This function will count the nodes of circular linked list  
    def countNodes(self):  
        current = self.head;  
        self.count=self.count+1;  
        while(current.next != self.head):  
            self.count=self.count+1;  
            current = current.next;  
        print("Count of nodes present in circular linked list: "),  
        print(self.count);  
      
   
class CircularLinkedList:  
    cl = CreateList();  
    #Adds data to the list  
    cl.add(1);  
    cl.add(2);  
    cl.add(4);  
    cl.add(1);  
    cl.add(2);  
    cl.add(3);  
    #Displays all the nodes present in the list  
    cl.countNodes();  


Count of nodes present in circular linked list:
6

Conclusion

We have seen one of the applications of circular linked lists in this tutorial. Also, the implementation part has been shown to produce the program's desired output.