Function to count the number of nodes in a linked list - recursive solution
int count(struct node *p)
// p is a pointer to the first node of the list.
{
if(p)
return 1 + count(p->link);
else
return 0;
}
Written by Munia Balayil
int count(struct node *p)
// p is a pointer to the first node of the list.
{
if(p)
return 1 + count(p->link);
else
return 0;
}
Written by Munia Balayil
int count(struct node *first)
// first is a pointer to the first node
{
struct node *p = first;
int count = 0;
while(p)
{
++count;
p = p->link;
}
// Final value of count gives the number of nodes
// in the linked list.
return count;
}
Written by Munia Balayil