【发布时间】:2015-09-12 23:25:14
【问题描述】:
在递归过程中,堆栈被创建,该堆栈包含什么,它包含值还是存储操作数的地址
void recursiveReverse(struct node** head_ref)
{
struct node* first;
struct node* rest;
/* empty list */
if (*head_ref == NULL)
return;
/* suppose first = {1, 2, 3}, rest = {2, 3} */
first = *head_ref;
rest = first->next;
/* List has only one node */
if (rest == NULL)
return;
/* reverse the rest list and put the first element at the end */
recursiveReverse(&rest);
first->next->next = first;
/* tricky step -- see the diagram */
first->next = NULL;
/* fix the head pointer */
*head_ref = rest;
}
在上面的代码中,休息指针保持最后一个节点的地址,而第一个指针不断变化,即它从堆栈中取值而休息指针没有。 所以首先我想了解递归堆栈,它的结构,它包含的内容,它是如何工作的,以及对上述代码的解释表示赞赏
【问题讨论】:
-
有一件重要的事情要知道,每当
recursiveReverse()方法被调用(递归地或从任何其他调用者)时,返回地址都会被压入堆栈。 -
这是C,所以参数的值和返回地址、返回值等都在栈上。看看网上的“栈帧”。
标签: c recursion linked-list stack