In this blog post we will learn how to count the number of nodes in a linked list using c/ c++ program. to count linked list nodes we have to traverse the linked list from head to tail. It is generally asked in programming job interview.

write a c program to count the number of linked list nodes.

For example in the below figure ,there are 5 nodes in the linked list.

how to count the number of nodes in a linked list

Sample code for how to count the number of nodes in a linked list 

C/C++ program:- ( iterative method)

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

// data structure for link list
typedef struct list {
	int data; //data
	struct list *next; // point to next node
} list;

list *head = NULL;

void insert(int a);

int count(list * head) {
	int count = 0;
	list *p = head;
	if (p == NULL) {
		return 0;
	}
	while (p != NULL) {
		count++;
		p = p->next;
	}
	return count;
}

int main() {
	// insert 1 to 5 in linked list
	int i = 0;
	for (i = 1; i < 6; i++) {
		insert(i);
	}
	// count
	int num_of_nodes = count(head);
	printf("there are %d nodes in linked list\n",num_of_nodes);
	return 0;
}

void insert(int a) {
	// create first node
	if (head == NULL) {
		head = (list *) malloc(sizeof(list));
		head->data = a;
		head->next = NULL;
	} else {
		// go to last node
		list *p = head;

		while (p->next != NULL)
			p = p->next;
		// create new node
		list *new_one = (list *) malloc(sizeof(list));
		new_one->data = a;
		p->next = new_one;
		new_one->next = NULL;
	}
}

C/C++ program:- ( recursive method):-

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

// data structure for link list
typedef struct list {
	int data; //data
	struct list *next; // point to next node
} list;

list *head = NULL;

void insert(int a);

int count(list * head) {
	int count = 0;
	list *p = head;
	if (p == NULL) {
		return 0;
	}
	while (p != NULL) {
		count++;
		p = p->next;
	}
	return count;
}
int count_r(list * head, int count) {
    if(head==NULL) return count;
    count_r(head->next,count+1);
}

int main() {
	// insert 1 to 5 in linked list
	int i = 0;
	for (i = 1; i < 6; i++) {
		insert(i);
	}
	// count
	int num_of_nodes = count_r(head,0);
	printf("there are %d nodes in linked list\n",num_of_nodes);
	return 0;
}

void insert(int a) {
	// create first node
	if (head == NULL) {
		head = (list *) malloc(sizeof(list));
		head->data = a;
		head->next = NULL;
	} else {
		// go to last node
		list *p = head;

		while (p->next != NULL)
			p = p->next;
		// create new node
		list *new_one = (list *) malloc(sizeof(list));
		new_one->data = a;
		p->next = new_one;
		new_one->next = NULL;
	}
}

How recursive method works here?

int count_r(list * head, int count) {
    if(head==NULL) return count;
    count_r(head->next,count+1);
}
  • In the count_r (list *head,int count) function, we call count_r again and again if there is next node available.
  • During each recursive call of count_r function , we are incrementing count variable and passing count in function argument.
  • Recursion ends when condition head==NULL becomes true, at this time we are returning count.

Ref:

https://stackoverflow.com/questions/22086128/counting-all-the-nodes-in-a-linked-list




Related Contents to follow