[英]passing pointer address in a recursive function in c
Im trying to understand the below code: 我试图理解以下代码:
void recursiveReverse(struct node **head_ref)
{
struct node *first;
struct node *rest;
if (*head_ref == NULL)
return;
first = *head_ref;
rest = first->next;
if (rest == NULL)
return;
recursiveReverse(&rest);
first->next->next = first;
first->next = NULL;
*head_ref = rest;
}
I noticed that the variable rest
is having the same value for all the recursion call once the code reached beyond the recursiveReverse(&rest)
. 我注意到,一旦代码超出了
recursiveReverse(&rest)
,则变量rest
对于所有递归调用都具有相同的值。 But first->next
has different values. 但是
first->next
具有不同的值。 I was able to understand why first->next
has different values by writing them on a stack and comparing it with each call. 通过将它们写入堆栈并将其与每个调用进行比较,我能够理解为什么
first->next
具有不同的值。 But i could not understand how rest
is having the same value for all the calls instead of the (rest = first->next)
from the stack. 但是我不明白
rest
所有调用如何具有相同的值,而不是堆栈中的(rest = first->next)
。 Please let me know if the question is not clear or if any details are needed. 如果问题不清楚或需要任何详细信息,请告诉我。 Thanks
谢谢
Update: I observed that, arranging the parameters properly, if i call recursivereverse(rest) instead of revursivereverse(&rest), the rest value changes for every recursive call just like any other variable on the revursion stack. 更新:我观察到,如果我正确地排列了参数,那么如果我调用recursivereverse(rest)而不是revursivereverse(&rest),则每次递归调用的rest值都会改变,就像revsion堆栈上的任何其他变量一样。 I could not understand what is the difference &rest is making in the call.
我不明白&rest在通话中有什么区别。
Consider the following input. 考虑以下输入。
1 2 3 4. 1 2 3 4。
First Recursion, 第一次递归
*Head_ref = 1;//value of head_Ref
first =1; // first=*head_ref;
rest=2;// rest=first->next;
Second recursion, 第二次递归
*Head_ref = 2;//value of head_Ref
first =2; // first=*head_ref;
rest=3;// rest=first->next;
Third Recursion. 第三递归。
*Head_ref = 3;//value of head_Ref
first =3; // first=*head_ref;
rest=4;// rest=first->next;
Fourth Recursion, 第四次递归
*Head_ref = 4;//value of head_Ref
first =4; // first=*head_ref;
rest=NULL;// rest=first->next;
Condition fails, It come to the third recursion , where it called. 条件失败,这是它调用的第三次递归。
Third Recursion, 第三递归
first=3;
first->next->next=first// here it means the rest link.
first->next=NULL;// it will make the pointer that is end.
*head_ref=rest; // making the starting address in this recursion
Now the list comes like this, 4 -> 3. Now the value of rest is changed into 4. 现在列表如下:4->3。rest的值更改为4。
Now it come to the second recursion, 现在到第二次递归,
Rest will pointing 4, but the first->next is pointing to 3. 其余的将指向4,但第一个->下一个将指向3。
first=2;
rest=4;
first->next->next=first// here it means the rest link.
first->next=NULL;// it will make the pointer that is end.
*head_ref=rest; // making the starting address in this recursion
So now the head_ref is pointing to 4. Then now the list will be 4 -> 3 -> 2. 因此,现在head_ref指向4。然后,列表将为4-> 3-> 2。
It comes to the first recursion, 涉及到第一次递归,
Here, 这里,
first=1,rest=4, But first -> next =2.
first->next->next=first// here it means the rest link.
first->next=NULL;// it will make the pointer that is end.
*head_ref=rest; // making the starting address in this recursion
At finally it change into 最终变成
4 -> 3 -> 2 -> 1.
So now the list is reversed. 因此,现在该列表被颠倒了。 Here main thing make the
*head_ref
into last position at end of the recursion. 在这里,最主要的是将
*head_ref
放在递归结束时的最后位置。
consider a linked list 1->2->3->4
. 考虑一个链表
1->2->3->4
。 As per the recursiveReverse()
, at the iteration of recursive function call when (rest == NULL)
is satisfied(node '4'), *head_ref
= 4;Now after this, the call returns to previous iteration(node '3'). 根据
recursiveReverse()
,在满足(rest == NULL)
(节点'4')时,在递归函数调用的迭代中, *head_ref
= 4;此后,该调用返回到先前的迭代(节点'3' )。 Here basically rest
(= '4') variable of the current iteration of function recursion(node '3') is actually *head_ref
of last iteration(last node '4') where *head_ref
was calculated as 4. hence at the end of the function in recursion(node '3'), we are doing *head_ref = rest;
这里,函数递归的当前迭代(节点“ 3”)的基本
rest
(=“ 4”)变量实际上是最后迭代( *head_ref
最后节点“ 4”)的*head_ref
,其中*head_ref
计算为4,因此在递归函数(节点“ 3”),我们正在做*head_ref = rest;
,ie. ,即。
*head_ref
= 4, since rest
is received as 4 from function iteration node = '4'. *head_ref
= 4,因为从函数迭代节点='4'接收rest
的4。 now at next consecutive recursive returns from these functions, address of *head_ref
is returned, which remains same through and hence the statement *head_ref = rest;
现在在这些函数的下一个连续递归返回中,返回
*head_ref
地址,该地址一直保持不变,因此语句*head_ref = rest;
gives the same values through out. 给出相同的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.