ProAnswers.org

How to count the number of elements in a linklist

write a function in C which returns the count of the number of elements present in the link list. All the elements are assumed to be integers.

typedef struct node
{
int data;
struct node *next;
}node;

int count_elem(node *head)
{
node *move;
int count=0;
move = head;
for(;move!=NULL;move=move->next)
{
count++;
}
return count;
}