gpt4 book ai didi

c - 如何使用双指针正确引用结构中的指针

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:40 25 4
gpt4 key购买 nike

我正在尝试创建一个链表,其中每个 ListNode 都包含一个二叉搜索树。以下是结构:

typedef struct TreeNode {
int label;
long count;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;

typedef struct ListNode {
TreeNode *ptr;
struct ListNode *next;
struct ListNode *prev;
} ListNode;

我有一个名为 addNode 的函数,它根据 TreeNode 的 labelcount 值之间的比较按顺序添加 ListNodes,但是我不知道如何正确比较他们。

我不断收到错误消息:请求成员“next”不是结构或 union
引用 addNode 中的第二个 if 语句:

void addNode(ListNode ** head, ListNode * new){

if(*head == NULL){
*head = new;
return;
}

if((*head -> next -> ptr -> count) < (new -> ptr -> count)){
addNode(&(*head -> next), new);
}

有人可以解释一下进行这种比较的正确方法吗?

最佳答案

-> 运算符的优先级高于一元 * 运算符。所以当你这样做时:

*head->next

你实际上是在说:

*(head->next)

您需要添加一些括号:

if(((*head) -> next ->  ptr -> count) < (new -> ptr -> count)){
addNode(&((*head) -> next), new);
}

关于c - 如何使用双指针正确引用结构中的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43684025/

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