w3resource

C++ Linked List Exercises: Count number of nodes in a linked list

C++ Linked List: Exercise-3 with Solution

Write a C++ program to create a singly linked list of n nodes and count the number of nodes.

Visualization:

C++ Exercises: Count number of nodes in a linked list.

Test Data:
Original Linked list:
13 11 9 7 5 3 1
Number of nodes in the said Linked list:
7

Sample Solution:

C++ Code:

#include <iostream>
 
using namespace std;
 
//Declare node 
struct Node{
    int num;
    Node *next;
};
 
//Starting (Head) node
struct Node *head=NULL;
 
//Insert node at start
void insert_Node(int n){
    struct Node *new_node=new Node;
    new_node->num=n;
    new_node->next=head;
    head=new_node;
}
int count_Nodes() {
  
 //Create a temporary node(tmp) pointing to the head.
  Node* tmp = head;

  // Count the nodes by creating a variable.
  int ctr = 0;
  
 // In the event that the temp node is not null, 
 // increase ctr by 1 and move on to the next node.
 // The process should be repeated until the temp is null.
  while(tmp != NULL) {
    ctr++;
    tmp = tmp->next;
  }

  //return ctr
  return ctr;  
} 
//Display all nodes
void display_all_nodes()
  {
	struct Node *temp = head;
    while(temp!=NULL){
        cout<<temp->num<<" ";
        temp=temp->next;
    }
    }
 
int main(){
    insert_Node(1);
    insert_Node(3);
    insert_Node(5);
    insert_Node(7);
    insert_Node(9);
    insert_Node(11);
    insert_Node(13);
    cout<<"Original Linked list:\n";
	display_all_nodes();
	cout<<"\nNumber of nodes in the said Linked list:\n";
    int ctr = count_Nodes();
    cout << ctr;
    cout<<endl;
    return 0;
}

Sample Output:

Original Linked list:
13 11 9 7 5 3 1 
Number of nodes in the said Linked list:
7

Flowchart:

Flowchart: Count number of nodes in a linked list.
Flowchart: Count number of nodes in a linked list.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Reverse linked list.
Next C++ Exercise: Insert a new node at the beginning of a Linked List.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

Program being compiled differently in 3 major C++ compilers. Which one is right?

GCC is correct, at least according to C++11 lookup rules. 3.4.3.1 [class.qual]/2 specifies that, if the nested name specifier is the same as the class name, it refers to the constructor not the injected class name. It gives examples:

B::A ba;           // object of type A
A::A a;            // error, A::A is not a type name
struct A::A a2;    // object of type A

It looks like MSVC misinterprets it as function-style cast expression creating a temporary C with y as a constructor parameter; and Clang misinterprets it as a declaration of a variable called y of type C.

Ref: https://bit.ly/3ww1Bna

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook