Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C++ program to count nodes in circular linked list

Count nodes in a circular linked list in C++

How to count nodes in circular linked list in C++?

To write a C++ program to count nodes in circular linked list, we need to initialize a node pointer and a variable that will store the count of nodes. Use the node pointer to point each node of the list and increase the count of the counter variable by one on every successful iteration. Stop the iteration when node pointer points the head.

In this article, we will study the steps and algorithm that will help us to write a C++ coded program for counting the nodes in a circular linked list.

Steps write a C++ program to count nodes in circular linked list

These steps are followed in CPP programming to count nodes in circular linked list

  1. Write set of codes to construct circular linked list.
  2. Create a function to build the circular linked list.
  3. Make a function to print the circular linked list data.
  4. Create a function to count the number of nodes present in circular linked list.
  5. In this function initialize a variable to count the number of nodes.
  6. Initialize a pointer that will point to each node on every successful iteration.
  7. Stop the iteration when pointer points the head again.
  8. Return the count of nodes.
  9. Now call these functions in the main function and print the total count of nodes.
Constuct circular linked list

How to build nodes of a in C++?

class Node  

    int data; 
    Node *next; 
}; 
C++ program to count nodes in circular linked list

Algorithm to count nodes in circular linked list in C++

This algorithm is followed in C++ programming to count nodes in circular linked list

  • INT CNT=0
  • STRUCT NODE CUR = HEAD
  • DO
  • CUR = CUR -> NEXT
  • CNT++
  • WHILE (CUR != HEAD)
  • RETURN CNT

C++ program for counting nodes in circular linked list

#include <iostream>
using namespace std;

struct node {
    int num;
    struct node * next;
}*head;
void build(int n)//function to build linked list
{
    int i, num;
    struct node *preptr, *newnode;

    if(n >= 1)
    {
        head = (struct node *)malloc(sizeof(struct node));

        cout<<"Enter data of the list:\n";
        cin>>num;
        head->num = num;
        head->next = NULL;
        preptr = head;
        for(i=2; i<=n; i++)
        {
            newnode = (struct node *)malloc(sizeof(struct node));
            cin>>num;
            newnode->num = num;
            newnode->next = NULL;	
            preptr->next = newnode;	
            preptr = newnode;   		
        }
        preptr->next = head; //linking last node with head node
    }
}
void display(struct node *head)//function to display list
{
    struct node *tmp;
    int n = 1;
    
    if(head == NULL)
    {
        cout<<"List is empty";
    }
    else
    {
        tmp = head;
        
        
        do {
            cout<<tmp->num<<" ";
            tmp = tmp->next;
            n++;
        }while(tmp != head);
    }
}
int count(struct node *head)//function to count number of nodes
{
    int cnt = 0;
    struct node *cur = head;

    //Iterating till end of list
    do 
    {
        cur = cur->next;
        cnt++;
    } while (cur != head);

    return cnt;
}

int main()//main function
{
    int n;
    struct node *head1 = NULL; 
    struct node *head2 = NULL;
    head = NULL;
    cout<<"Enter the size of circular linked list: ";
    cin>>n;
    build(n); 
    cout<<"\nCircular linked list data:\n";
    display(head);
    cout<<"\nTotal number of nodes are: "<<count(head);
}
Output:
Enter the size of circular linked list: 5
Enter data of the list:
10
20
30
40
50

Circular linked list data:
10 20 30 40 50 
Total number of nodes are: 5

Want to learn more Data Structure?