递归到迭代

时间:2011-04-27 01:47:56

标签: c

我有这个功能,我想把它改成迭代的。有谁知道怎么做?

#include "list.h"

int count(LINK head)
{

if(head == NULL)
   return 0;
else
   return (1 + count(head -> next));
}

1 个答案:

答案 0 :(得分:5)

int count(LINK head)
{
    int count = 0;
    while(head != NULL)
    {
        head = head->next;
        count = count + 1;
    }
    return count;
}