Counting number of Nodes in Singly Linked List

April 22, 2025 1 Comment » Hits : 863





Counting number of Nodes in Linked List : This Function is similar to display function , only difference is that instead of printing data we are incrementing length variable .


  1. Using Non Recursion
  2. Using Recursion

Program : Way 1 [ Does not Returning Value ]

void count()
{
    struct node *temp;
    temp=start;
    while(temp!=NULL)
    {
        length++;
        temp=temp->next;
    }
printf("\nLength of Linked List : %d",length);
}

Program : Way 2 [ Returning Value ]

int count()
{
    struct node *temp;
    temp=start;
    while(temp!=NULL)
    {
        length++;
        temp=temp->next;
    }
return(length);
}

Program : Way 3 [ Recursive Program to Count Number of Nodes in Linked List ]

int count(node *temp)
{
 if(temp==NULL)
     return(0);
 return(1 + count(temp->next));
}

Incoming search terms:

  • Anonymous

    gr8 article..