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 .
- Using Non Recursion
- 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)); }