How to count nodes in a simple linked list in C++
A linked list is a linear data structure in which elements, called nodes, are connected through pointers. Every node in the series has data as well as a reference (pointer) to the next node. The first node is called the head node. The last node’s pointer points to null, indicating the end of the list.
Problem statement
Demonstrate the creation of a singly linked list, and the calculation of the total number of nodes in the linked list.
Solution
Initialize
count = 0and a temporary variablecurrwith the head of the linked liststart.Traverse the list and count nodes.
Stop the loop when the temporary variable becomes equal to
None.Return the
count.
#include <iostream>class Node{public: int data;Node * next;Node(int data){this->data = data;this->next = nullptr;}};int countNodes(Node *start){int count = 0;Node *curr = start;while (curr){count++;curr = curr->next;}return count;}int main(){Node *start = new Node(19);start->next = new Node(22);start->next->next = new Node(33);int nodeCount = countNodes(start);std::cout << "Count of nodes is " << nodeCount << std::endl;// Don't forget to free memory used by the linked listNode *curr = start;while (curr != nullptr){Node *temp = curr;curr = curr->next;delete temp;}return 0;}
Explanation
Lines 3–13: It defines a class called
Node. It’s meant to represent a node in a singly linked list. Each node has an integerdatavalue and a pointer to thenextnode in the list. The constructor initializesdataand sets thenextpointer tonullptr.Line 15: It declares a function called
countNodes. It takes a pointer to aNodecalledstartas its argument and returns an integer.Line 17: It initializes an integer variable named
countand sets it to0. This variable will be used to keep track of the number of nodes in the linked list.Line 18: It initializes a pointer to a
Nodenamedcurrand sets it to the same node as thestartnode. This pointer is used to traverse the linked list.Line 20: It starts a
whileloop that continues as long as thecurrpointer isn’t pointing tonullptr, which means there are more nodes to traverse in the linked list.Line 22: It increments the
countvariable by 1 for each node encountered. This effectively counts the nodes in the linked list.Line 23: It updates the
currpointer to point to the next node in the linked list. It follows thenextpointer of the current node to move to the next node in the list for the next iteration.Line 26: It returns the
countvariable, which holds the count of nodes in the linked list.Lines 31–33: It creates a linked list. Three nodes with data values 19, 22, and 33 are created, and their “next” pointers are set to form a linked list.
Line 35: The
countNodesfunction is called to count the number of nodes in the linked list, and the result is stored in thenodeCountvariable.Line 37: It prints the count of nodes to the standard output.
Lines 40–46: It’s used for cleaning up the dynamically allocated memory used by the linked list. It iterates through the list, deleting each node and updating the
currpointer until the entire list is deallocated.
Free Resources