Back to: Data Structures and Algorithms Tutorials

**How to Count Nodes in a Linked List using C Language**

In this article, I am going to discuss **How to Count Nodes in a Linked List using C Language** with Examples. Please read our previous article, where we discussed the **Recursive Function for Displaying a Linked List in C** with Examples.

**How to Count Nodes in a Linked List:**

Here, we will create a function for counting the number of nodes in a linked list i.e. length of the linked list.

We have to traverse through the linked list until we reach the last node. And every time we have to count the node. So, in this way, we can know the number of nodes in the linked lists.

**int count = 0;**

We have to take one variable that stores the number of counts in a linked list, so here we have taken â€˜countâ€™ and initialized it to 0. Now p is pointing on the 1^{st} node, so increment the count and move â€˜pâ€™ to the next node. Then â€˜pâ€™ will point on the 2^{nd} node, so increment the count and move â€˜pâ€™ to the next node, and so on. When â€˜pâ€™ became null then stop there. This is the algorithm for counting the number of nodes. So, for this let us write a function,

int count(struct Node *p){ int count = 0; while(p! = 0){ count++; p = p -> next; } return (count); }

This is the function for calculating the length of a linked list or counting the number of nodes. Now let us analyze the time taken by this function. This function is traveling through all the nodes and simply counts. So how much time does it take for traversing all the nodes? It depends on the number of nodes. So, if n nodes are there then we will say time is O (n).

So mostly functions that are written upon linked list will be taking the order of n time. This was the iterative function to count the number of nodes. Now let us write a recursive function for counting.

**Recursive Function 1 for Counting Nodes in a Linked List using C Language:**

int Rcount(struct Node *p){ if(p == 0) return 0; else return Rcount(p->next) + 1; }

The base condition for this recursive function is that â€˜pâ€™ should not be null. If â€˜pâ€™ is null then return 0 otherwise we will call the function itself with â€˜p->nextâ€™ as a parameter and add 1 to this. Let us trace this recursive function:

**Tracing Tree of Recursive Function for Counting Nodes in a Linked List:**

First, â€˜Count(200)â€™ will be called as â€˜pâ€™ point on the address 200. Then it will check for the condition â€˜if(p == 0)â€™. In this case, â€˜pâ€™ is not null so it will enter in the else part. In else we will make recursive call with parameter â€˜Count(p->next) + 1â€™. In recursive call â€˜pâ€™ is storing the address of the next node. Then we add â€˜1â€™ to the result of the recursive call. So, the first result has to be calculated for the recursive call then only we can add â€˜1â€™. The Addition will perform at the return time of the calls. Below is the complete tracing tree.

So, when all the nodes will traverse then â€˜pâ€™ will point to null. When â€˜pâ€™ will point to null then we simply return â€˜0â€™. When all the calls will be executed then the last call will return 0. Now from here, the addition will perform.

As you can see in the tree, â€˜Count(0)â€™ will return 0, then this 0 will be added to the â€˜1â€™ of call â€˜Count(230)â€™. So result â€˜0 + 1 = 1â€™ will be returned to the previous call â€˜Count(230)â€™.

Here â€˜1â€™ is added to the result of the call â€˜Count(230)â€™ that is â€˜1 + 1 = 2â€™ and â€˜2â€™ will be returned to the previous call â€˜Count(270)â€™. In the same way, the addition will be performed in the rest of the calls. Below is the complete tracing tree:

So, we will get â€˜4â€™ as the final answer. So, there are â€˜4â€™ nodes present in the linked list. This recursive function will take,

**Time Complexity: O(n)**

**Space Complexity: O(n)**

**Recursive Function 2 for Counting Nodes in a Linked List using C Language:**

int Rcount2(struct Node *p){ int x = 0; if(p){ x = RCount2(p->next); return x + 1; } else return x; }

This is an alternative recursive version to calculate the length of the linked list.

**Program for Counting the Number of Nodes in a Linked List using C Language:**

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; } *first = NULL; void create(int A[], int n) { int i; struct Node *t, *last; first = (struct Node *) malloc (sizeof (struct Node)); first->data = A[0]; first->next = NULL; last = first; for (i = 1; i < n; i++) { t = (struct Node *) malloc (sizeof (struct Node)); t->data = A[i]; t->next = NULL; last->next = t; last = t; } } int count(struct Node *p) { int c = 0; while (p) { c++; p = p->next; } return c; } int Rcount(struct Node *p) { if (p == 0) return 0; else return count (p->next) + 1; } int Rcount2(struct Node *p) { int x = 0; if (p) { x = Rcount2 (p->next); return x + 1; } else return x; } int main() { int A[] = { 8, 3, 7, 12 }; create (A, 4); printf ("Count %d\n", count (first)); return 0; }

**Output:**

In the next article, I am going to discuss how to find the **Sum of all elements in a Linked list in C Language** with Examples. Here, in this article, I try to explain **How to Count Nodes in a Linked List using C Language** with Examples and I hope you enjoy this Counting Node in a Linked List using C Language with Examples article.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.