0

我在理解这种结构时遇到了问题,很想得到一个清晰的解释。

typedef struct exp{
   int x;
   struct exp *parent;
   struct exp **children;
}

父母和孩子是什么意思?“父”是这个结构的数组?孩子们的意思是什么?它是一个数组数组?!实在看不懂。。

最后一件事,如果我添加一个元素,它会成为某个父母的特定孩子,我怎样才能接触到父母的所有孩子?它不应该是一个结构“列表”(使用下一个等 .. 吗?)?

谢谢你!!

4

5 回答 5

4

此图像显示了一种可能的情况:

一个可能的场景

它是使用此代码和DDD获得的

#include <stdio.h>
#include <stdlib.h>

struct exp{
    int x;
    struct exp *parent;
    struct exp **children;
};

int main ()
{
    struct exp *x = calloc(1, sizeof(x[0]));
    x->x = 42;
    x->parent = calloc(1, sizeof(x[0]));
    x->children = calloc(5, sizeof(x->children[0]));
    x->children[0] = calloc(1, sizeof(x[0]));
    x->children[2] = calloc(1, sizeof(x[0]));
    x->children[3] = calloc(1, sizeof(x[0]));
    x->children[4] = calloc(1, sizeof(x[0]));
    return 0;
}

基本上,该children字段是指向的指针向量struct exp。你决定放多少元素和其他东西。

PS:代码只是一个demo,质量不太好。

于 2012-07-11T14:58:19.843 回答
3

它是一个指向指针的指针,在这种情况下,它似乎被用作struct exp.

每个struct exp都有一个对它的“父”的引用,以及一个指向子列表的指针struct exp

typedef struct exp{
    int x;
    struct exp *parent;
    struct exp **children;
} element;

// create ROOT elemnt
element * root = (element*) malloc(sizeof(element)); //alocate mem. for 1 element

一旦我们有了“根”,我们就可以添加孩子,以下是伪代码

for 1 to 10{
    child = new element;
    child->parent = root;                // tell the child who is his parent
    addToRoot( root , child);            // call a function that inserts elemnts to root

}

所以现在我们应该有root一个包含 10 个元素的列表:

_______________                                    _______________   
|             | (children)                         |             | - (parent) points to struct exp, root
|     root    | - points to list of struct exp  -> |   child 0   |   
|             |                                    |             | - (children) points to null; // if it's empty  
_______________                                    _______________ 

                                                   _______________  
                                                   |             | - (parent) points to struct exp, root
                                                   |   child 1   |     
                                                   |             | - (children) points to null; // if it's empty 
                                                   _______________ 

                                                   _______________  
                                                   |             | - (parent) points to struct exp, root 
                                                   |   child 2   |      
                                                   |             | - (children) points to null; // if it's empty  
                                                   _______________ 

                                                   _______________  
                                                   |             | - (parent) points to struct exp, root 
                                                   |   child 3   |      
                                                   |             | - (children) points to null; // if it's empty  
                                                   _______________ 

                                                         .
                                                         .
                                                         . 

                                                   _______________  
                                                   |             | - (parent) points to struct exp, root 
                                                   |   child 9   |      
                                                   |             | - (children) points to null; // if it's empty  
                                                   _______________    

类似的东西......它有帮助吗?

于 2012-07-11T14:57:03.823 回答
1

它是一个指向子节点的指针数组——这看起来像是某种树结构。

每个节点都有一个父节点,每个节点有一个或多个子节点。您可以从父母前往其中一个孩子

expInstance->children[i];

其中 i 是表示其中一个子节点的数字。从这个定义中并不清楚有多少子节点——它可能是一个、两个或一百万。但是鉴于这些信息,您可以遍历它们。要么与

for(i=0; i<NUMBER_OF_CHILDREN_NODES;i++){
    expInstance->children[i];
}

如果您提前知道数组的长度或有些奇怪

while(expInstance.children[i++]){
    expInstance->children[i];
}

(有几种不同的巧妙方法可以做到这一点,但必须在结构中建立这样的假设,即在该数组的最后一个槽中将有一个空指针以终止它。)

于 2012-07-11T14:39:01.077 回答
0

没有看到更多代码很难说。你想实现一棵树吗?从这个定义来看,这对我来说是显而易见的:

  • parent 指向此结构的父节点。
  • children 指向其所有子节点的指针数组。
于 2012-07-11T14:37:10.510 回答
0

首先,请记住,结构不能包含自身的实例。IOW,你不能做类似的事情

struct exp {
  int x;
  struct exp parent;
  ...
};

主要原因是结构类型在您尝试声明成员时不完整;由于您还没有完成对类型的描述,编译器不知道该成员应该有parent多大。parent更不用说parent成员本身会有一个parenttype 的成员struct exp,而后者又会有一个parenttype 的成员struct exp,而后者又会有一个parenttype 的成员struct exp,等等等等等等。你会结束的与需要无限量存储的对象。

所以你不能有 type 的成员struct exp。但是,您可以有一个指向类型对象的指针struct exp;您可以创建指向不完整类型的指针,并且所有结构指针类型都具有相同的大小和表示形式(IOW,struct foo *pf并且struct bar *pb将具有相同的大小,即使struct foostruct bar不具有相同的大小)。

所以parent成员(可能)只意味着指向一个类型的实例struct exp(每个实例struct exp都有一个父级)。同样,该children成员(可能)是一个指向指针的单维数组struct exp(每个实例atruct exp可以有零个、一个或多个子级)。

于 2012-07-11T19:26:02.917 回答