Sunday, November 20, 2005

Counting a list iteratively

This is a simple way of counting elements in a list. For some, they would prefer to embed this operation upon list creation and insertion or deletion.



int count_it(LINK head)
{
        int cnt = 0;

        for ( ; head != NULL; head = head -> next)
                ++cnt;

        return cnt;
}

No comments: