gpt4 book ai didi

c - curr_node.ptr = &(next_node.ptr); 之间的区别和 curr_node.ptr = &next_node

转载 作者:行者123 更新时间:2023-11-30 20:20:20 25 4
gpt4 key购买 nike

示例#1

#include <stdio.h>

struct node{
struct node *next;
char *data;
};

struct node *reverse_list(struct node *head)
{
struct node *curr, *prev, *next;
curr = head;
prev = NULL;
next = curr->next;
while(curr != NULL){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}

int main(){
struct node *reverse_list();
return 0;
}

我的理解:

这段代码描述了一个链接,每个指针都指向下一个节点的值(或节点的开头),某种程度上它是一个基本且简单的结构。但是如果我们希望所有的指针都指向下一个指针,然后反转呢?当所有指针都指向指针时,如果我们想反转它,这个过程就显得很复杂。

也就是说,

curr_node.ptr = &(next_node.ptr);

不是:curr_node.ptr = &next_node

最佳答案

curr_node.ptr = &(next_node.ptr) 获取 next_nodeptr 的地址,其中 curr_node.ptr = &next_node 获取next_node的地址

如果您想反转链接列表,只需谷歌搜索以下内容即可

How to reverse a linked list in C

rev_list.c on github something like that.

希望有帮助。

尼古拉斯

关于c - curr_node.ptr = &(next_node.ptr); 之间的区别和 curr_node.ptr = &next_node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46577042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com