C program to count nodes in Circular Linked List

Write a C program to count nodes in Circular Linked List. In this example I will explain how to count nodes in Circular Linked List. C program to create a Circular Linked List and count total elements in Circular Linked List.

Logic to count nodes in a Circular Linked List

Step by step descriptive logic to count nodes in a circular linked list.

1. Create a Circular Linked List and assign reference of first node to head.
2. Initialize `count = 0;` variable to store total nodes in list.
3. Initialize another variable to traverse list, say `current = head;`.
4. Increment `count++` and `current = current->next;`.
5. Repeat step 4 till you reach head node after traversing once.

Program to count nodes in Circular Linked List

``````/**
* C program to count nodes in a Circular Linked List
*/

#include <stdio.h>
#include <stdlib.h>

/* Basic structure of node */
struct node {
int data;
struct node * next;
};

/* Function declaration */
void createList(struct node **head, int n);

int main()
{
int n, choice;

/*
* Run forever until user chooses 0
*/
while(choice != 0)
{
printf("--------------------------------------------\n");
printf("        CIRCULAR LINKED LIST PROGRAM        \n");
printf("--------------------------------------------\n");
printf("1. Create List\n");
printf("2. Display list\n");
printf("3. Count nodes\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");

scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter total node to create: ");
scanf("%d", &n);
break;

case 2:
getchar(); // Hold screen
getchar(); // Hold screen
break;

case 3:
getchar(); // Hold screen
getchar(); // Hold screen
break;

case 0:
printf("Exiting from application");
exit(0);
break;

default:
printf("Error! Invalid choice. Please choose between 0-3");
}

printf("\n\n\n\n\n");
}

return 0;
}

/**
* Function to count total number of nodes in Circular Linked List
*/
{
int total = 0;

// Iterate till end of list
do
{
current = current->next;
total++;

}

/**
* Creates a circular linked list of n nodes.
*/
void createList(struct node **head, int n)
{
int i, data;
struct node *prevNode, *newNode;

prevNode = NULL;
newNode  = NULL;

/* Creates and links rest of the n-1 nodes */
for(i=1; i<=n; i++)
{
// Create a new node
newNode = (struct node *) malloc(sizeof(struct node));

printf("Enter data of %d node: ", i);
scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;

// Link the previous node with newly created node
if (prevNode != NULL)
prevNode->next = newNode;

// Move the previous node ahead
prevNode = newNode;

}

// Link last node with first node

}

/**
* Display node content of circular linked list
*/
{
struct node *current;
int n = 1;

// Nothing to print in list
{
printf("List is empty.\n");
return;
}

printf("DATA IN THE LIST:\n");

do
{
// Print current node
printf("Data %d = %d\n", n++, current->data);

// Move to next node
current = current->next;
}``````
```--------------------------------------------
--------------------------------------------
1. Create List
2. Display list
3. Count nodes
0. Exit
--------------------------------------------
Enter total node to create: 5
Enter data of 1 node: 10
Enter data of 2 node: 20
Enter data of 3 node: 30
Enter data of 4 node: 40
Enter data of 5 node: 50

--------------------------------------------
--------------------------------------------
1. Create List
2. Display list
3. Count nodes
0. Exit
--------------------------------------------
DATA IN THE LIST:
Data 1 = 10
Data 2 = 20
Data 3 = 30
Data 4 = 40
Data 5 = 50

--------------------------------------------
--------------------------------------------
1. Create List
2. Display list
3. Count nodes
0. Exit
--------------------------------------------
Total nodes = 5

--------------------------------------------