# Count nodes in circular linked list

## How to count nodes in circular linked list in C?

To write a C program to count nodes in circular linked list, we need to initialize a node pointer and a variable that will store the count of nodes. Use the node pointer to point each node of the list and increase the count of the counter variable by one on every successful iteration. Stop the iteration when node pointer points the head.

In this article, we will study the steps and algorithm that will help us to write a C coded program for counting the nodes in a circular linked list.

## Count nodes in circular linked list

In this topic we will discuss about the how many nodes have in a circular linked list it means count nodes in circular linked list.

we have to find out the number of nodes present in the circular linked list,firstly we create the circular linked list, then traverse through the list and increment variable ‘count’ by 1.

In order to find the length of the linked list, we will be using traversal techniques.

Traversing the linked list till the end node gives us the count of the number of nodes.  The only thing we need to take care is that in a circular linked the last node doesn’t point to NULL instead it points to head thus we need to compare with the head pointer and traverse until current nodes next is head.

### Algorithm for count nodes in circular linked list

1-   Accept the head pointer in the function.
2-   Declare the count variable which maintains the count of nodes and initialize it to zero.

4- 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.

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

### Code For Count Nodes in Circular Linked List

Run
```#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};

int count(struct Node *head)//function to count number of nodes
{
int cnt = 0;

//Iterating till end of list
do
{
cur = cur->next;
cnt++;

return cnt;
}

void insertStart (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;

// if its the first node being entered
{
return;
}

// traverse till last node in LL
{
curr = curr->next;
}

curr->next = newNode;
}

{
// if there are no node in LL
return;

//need to take care of circular structure of LL
do
{
printf ("%d ", temp->data);
temp = temp->next;

}
printf ("\n");
}

int main ()				//main function
{

printf("Total number of nodes are: %d",count(head));

return 0;
}
```

### Output:

```Linked List is: 1 2 3 4 5 6
Total number of nodes are: 6
```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Circular Linked List
• Circular Linked List in –
C | C++ | Java
• Insertion in Circular Linked List –
C | C++ | Java
• Insertion at the beginning–
C | C++ | Java
• Insertion at the end –
C | C++ | Java
• Insertion at nth position –
C | C++ | Java
• Deletion in Circular Linked List –
C | C++ | Java
• Deletion from beginning in Circular Linked List –
C | C++ | Java
• Deletion from nth position in Circular Linked List –
• Deletion from end in Circular Linked List –
C | C++ | Java
• Insertion and Deletion in Circular Linked List – C | C++ | Java
• Split a Circular Linked List in two halves –
C | C++ | Java
• Count nodes in Circular Linked List –
C | C++ | Java
• Sorted Insert In Circular Linked List –
C | C++ | Java
• Insertion in the middle in Circular Linked List –
C | C++ | Java