Saturday, September 24, 2011

Function to count the number of nodes in a linked list - iterative solution


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

No comments:

Post a Comment