2011-04-27 119 views
0

我有这个功能,我想将它改为迭代的。有谁知道该怎么做?递归迭代

#include "list.h" 

int count(LINK head) 
{ 

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

这气味像功课给我。你甚至尝试过吗? – 2011-04-27 01:51:26

+1

是的,我知道该怎么做,如果你尝试过,你也可以。 – 2011-04-27 02:17:09

+0

[可以将每个递归转换为迭代?](http://stackoverflow.com/q/931762/),[将递归算法转换为迭代算法的设计模式](http://stackoverflow.com/q/1549943 /) – outis 2012-06-20 22:43:16

回答

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