更新时间:2022-10-22 21:12:53
我觉得这应该工作(但要注意,它需要一个额外的参数跟踪开始):
诠释计数(节点*开始)
{
返回count_helper(启动,启动);
}
INT count_helper(节点*电流,节点*启动)
{
INT℃;
C = 0;
如果(当前== NULL)
返回0;
如果((电流 - > roll_no)== 20)
C = 1;
如果(电流 - >接下来==开始)返回℃;
返回(C + count_helper(电流 - >接着,启动));
}
I know people usually ask this question the other way round, but I have the following problem: I have this iterative function which counts all the nodes in a circular doubly link list containing the data value 20. Now, how do I make this recursive, what will be the base case (terminating case) for the recursive function? Any help is appreciated:
int count(node *start)
{
int c;
c = 0;
if(start == NULL)
return 0;
if((start->roll_no) == 20)
c = 1;
node *current = start;
while(current->next != start)
{
if((current->next->roll_no) == 20){
c++;
}
current = current->next;
}
return c;
}
I think this should work (but note that it requires an extra argument for tracking start):
int count(node *start)
{
return count_helper(start, start);
}
int count_helper(node *current, node *start)
{
int c;
c = 0;
if(current == NULL)
return 0;
if((current->roll_no) == 20)
c = 1;
if(current->next == start) return c;
return (c + count_helper(current->next, start));
}