gpt4 book ai didi

c++ - 访问链表中的结构(带有指向其子实现的指针列表的树)

转载 作者:行者123 更新时间:2023-11-28 04:16:27 25 4
gpt4 key购买 nike

<分区>

我需要实现 3 个函数(addElem、member 和 findPathCost),这些函数对一棵树进行操作,该树包含指向带有链表的子节点的指针列表。 struct treeNode 定义树中的节点,childrenListElem 定义树节点的子节点列表。

struct tree::treeNode {
Label label;
Weight weight;
childrenList children; //pointer to the list of its children
};

struct tree::childrenListElem {
treeNode* child; //pointer to the first element of the children's list
childrenListElem* next; //pointer to the next one
};

在标题中:

struct treeNode;             // forward declaration
typedef treeNode* Tree; // pointer to root of tree
const Tree emptyTree = NULL; // empty tree

struct childrenListElem; // forward declaration
typedef childrenListElem* childrenList;
const childrenList emptyChildrenList = NULL; // empty children list

我的问题是我无法从 treeNode 结构访问子列表,这是我创建的 addAlemen 和成员的辅助函数中的示例:

//AUXILIARY FUNCTION: getNode(Label, Tree) returns the node with the given label in the tree.
//Used both in addElem and in member.

Tree getNode(Label & l, const Tree t)
{
Tree aux = t;
while (!isEmpty(aux)) {
if (aux->label == l)
return aux;
aux = (aux->children)->next; //HERE IS MY PROBLEM:
//usually I would have just done
//aux = aux->NextVertex
//(with NextVertex being the next
//treeNode in the tree t);
//but I can't seem to access the
//second struct as the compiler
//tells me that "children" is
//apparently not a pointer.
//How can I access the second struct?
}
return emptyTree;
}

这是我的编译器显示的错误:

 The error is: "error: cannot convert 'tree::childrenListElem*' to 'tree::Tree' {aka 'tree::treeNode*'} in assignment aux = aux->children->next;"

当我改为放置 aux = aux->children.next;我看到这个错误(我正在使用 gcc 编译器):

The error is: "error: request for member 'next' in 'aux->tree::treeNode::children', which is of pointere type 'tree::childrenList' {aka 'tree::childrenListElem*'} (maybe you meant to use '->'?)

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