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



我需要实现 3 个函数(addElem、member 和 findPathCost),这些函数在树上运行,树上有指向带有链表的子项的指针列表。结构体 treeNode 定义树中的节点,子项列表Elem定义树节点的子节点列表。

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 '->'?)
(

已编辑)简短回答:

在尝试编译您的示例后,我注意到,children 实际上不是 childrenListElem 的实例,而是一个指针,您访问它的方式是正确的。抱歉,对这些类型定义有点困惑。 但是您尝试分配值

(aux->children)->next 

其类型为 childListElem* 到类型为 Tree 又名 treeNode* 的变量。将其更改为后

aux = (aux->children)->child;

它在我的电脑上编译,也使用 GCC。

(旧的和错误的)简短的回答:

使用点运算符。

aux = aux->children.next;

(现在错了)解释:

如果我们分解这一个语句,它就会变成这样:

aux = aux->children->next;
//can also be written like the following to lines:
childrenList child = aux->children;
//1. dereference aux and retrieve the member children
//the result is instance of childrenList
aux = child->next
//2. dereference child and retrieve member next.
//this fails, because child is not a pointer

(有点可能仍然有帮助) 扩展答案:

也许您只是将"->"运算符与"."-运算符,这对我来说经常发生:) 或者您对指针和成员有普遍的误解。在这种情况下,也许这个简短的例子会对你有所帮助:

struct a;
struct b
{
//this is a full instance of a
a instanceOfA;
// this is just a pointer to an instance of a
a* pointerToA;
}
void foo()
{
b instanceOfB;
//members of an instance are always accessed with the dot-operator:
a instanceOfA = instanceOfB.instanceOfA;
a *pointerToA = instanceOfB.pointerToA;
//if you have a pointer you need to use the -> operator
b *pointerToB = new b();
instanceOfA = pointerToB->instanceOfA;
pointerToA = pointerToB->pointerToA;
//The -> operator is just the shortcut for dereferencing + member access
//It does the same like this:
instanceOfA = (&pointerToB).instanceOfA;
pointerToA = (&pointerToB).pointerToA;
}

我希望这可以解决您的问题,并澄清一些细节:)

最新更新