2011-04-27 118 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; 
}