0

この構造を理解するのに問題があります。明確な説明が必要です。

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

親と子はどういう意味ですか?「親」はこの構造の配列ですか?そして、子供たちの意味は何ですか?それは配列の配列ですか?!本当に分かりません。

最後に、要素を追加している場合、それはある親の特定の子になりますが、どうすれば親のすべての子に到達できますか?それは構造「リスト」であるべきではありません(nextなどを使用して..?)?

ありがとうございました!!

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: コードは単なるデモであり、品質はあまり高くありません。

于 2012-07-11T14:58:19.843 に答える
3

これはポインタへのポインタであり、その場合は のリストとして使用されているようですstruct exp

それぞれstruct expに、その「親」への参照と、 children のリストへのポインタがあります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

これは、子ノードへのポインターの配列です。これは、ある種のツリー構造のように見えます。

ノードごとに親ノードがあり、各ノードには 1 つ以上の子があります。親からその子の 1 つに移動できます

expInstance->children[i];

ここで、i は子ノードの 1 つを示す番号です。この定義から、子ノードがいくつあるかは明確ではありません。1 つ、2 つ、または 100 万になる可能性があります。しかし、その情報があれば、それらをループすることができます。どちらか

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

配列の長さを事前に知っているか、やや奇妙な

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

(これを行うには、さまざまな巧妙な方法がいくつかありますが、この配列を終了するために、この配列の最後のスロットに null ポインターがあるという前提が構造に組み込まれている必要があります。)

于 2012-07-11T14:39:01.077 に答える
0

もっと多くのコードを見ずに言うのは難しいです。ツリーを実装しようとしていますか? この定義から、これは私には明らかです。

  • 親は、この構造体の親ノードを指します。
  • children は、そのすべての子ノードのポインターの配列を指します。
于 2012-07-11T14:37:10.510 に答える