c - 递归反向链表
问题描述
我很难理解这个递归代码是如何工作的,我已经绘制了图纸并通过gdb
.
void RecursiveReverse(struct node** headRef)
{
struct node* first;
struct node* rest;
if (*headRef == NULL) return; // empty list base case
first = *headRef; // suppose first = {1, 2, 3}
rest = first->next; // rest = {2, 3}
if (rest == NULL) return; // empty rest base case
RecursiveReverse(&rest); // Recursively reverse the smaller {2, 3} case
// after: rest = {3, 2}
first->next->next = first; // put the first element on the end of the list
first->next = NULL;
*headRef = rest; // fix the head pointer
}
我知道,在建立递归调用堆栈并且列表仅包含 {3} 时,空的其余基本情况 if (rest == NULL)
是true
第一次。
在此之后,递归调用堆栈开始中断并first->next->next = first;
第一次命中,{2, 3},
在执行此行之前,输出gdb
:
(gdb)p *first
{data = 2, next = 0x1003001f0}
(gdb) p *rest
{data = 3, next = 0x0}
这一行执行后,
(gdb) p *rest
{data = 3, next = 0x1003000a0}
继续执行代码到 hit first->next->next = first;
,第二次:
(gdb) p **head_ref
{data = 1, next = 0x1003000a0}
(gdb) p *rest
{data = 3, next = 0x1003000a0} // expected p *rest to be 2
在这里,我希望本地指针rest
应该指向节点 2,因为在构建递归调用堆栈时,**headRef
指向节点 1 和 line 之后rest = first->next;
,被执行rest
指向节点 2。
, 执行后*headRef = rest;
,不应该headRef
指向节点2吗?
为什么本地状态丢失并且休息点指向节点 3?
解决方案
假设您有一个列表,并且它的其余部分已经反转。
在反转其余部分之前,列表具有这种结构
first -> first_of_rest -> second_of_rest->...->nth_of_rest->nullptr
反转其余部分后,您将获得
first -> nullptr <- first_of_rest <- second_of_rest <-...<-nth_of_rest
| |
________________________________________________________
the rest part of the list
所以节点的数据成员nextfirst
指向,first_of_rest
而节点的数据成员next first_of_rest
“指向”nullptr。
所以此时我们需要做的是设置节点的数据成员first_of_rest
指向节点first
first->next->next = first;
abd 将节点的数据成员 next 设置first
为“指向”nullptr。
first->next = NULL;
因此我们有
nullptr <-first <- first_of_rest <- second_of_rest <-...<-nth_of_rest
推荐阅读
- ruby - 如何运行 cronjobs 和 rackup
- amazon-web-services - 如何在 AWS 中下载我的公钥(密钥对)?
- reactjs - 尝试导入错误:“模型”未从“../模块”导出
- java - 单引号内未替换命名参数
- cuda - openACC 例程中的 cuSPARSE 库调用
- ios - 单个 BLE 中央可以通过低功耗蓝牙连接多个 BLE 外设吗?
- r - 在ggplot中添加手动比例时如何修复错误?
- postgresql - 无法使用 Postgres FDW 访问外部表
- python - z3 中 GADT 的意外行为,得到的值等于每个整数
- python - Numpy 索引广播引入了新维度