【发布时间】:2016-05-19 10:01:08
【问题描述】:
在下面的代码 sn-p 中,count 函数计算创建的链表中的节点数。我想知道下一个地址是如何从count(C_list->next);函数调用传递过来的?
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node;
main()
{
head = (node*)malloc(sizeof(node));
create(head);
print(head);
c = count(head);
}
int count(node* C_list)
{
if(C_list->next==NULL)
return(0);
else
{
return(1+count(C_list->next));//How does the next address gets passed from this function call?
}
}
【问题讨论】:
-
你在问这个(递归)是如何工作的吗?您的代码已经发送了下一个地址。你的程序的输出是什么?
-
请注意,这是一种非常低效的混淆计算链表节点的方法。专业代码不会使用递归,而是使用普通循环。递归几乎总是解决实际编程问题的一种非常糟糕的方法。
标签: c singly-linked-list