このデータは、next という自分自身と同じデータ構造へのpointerをもち、さらに field0, field1 という データ領域も持っています。 こういった場合は、単に
struct ListA { struct ListA * next; int field0; double field1; };などと書くだけです。typedef も一緒にしたいなら、
typedef struct ListA { struct ListA * next; /* まだ typedef は終わっていないので */ int field0; double field1; } ListA_t, * ListA_tp; ------ あるいは ------- typedef struct ListA ListA_t, * ListA_tp; /* 先に typedef しましょ */ struct ListA { ListA_tp next; int field0; double field1; };という感じです。データ構造のイメージさえ描けていれば、なにも難しいことはありません。 アクセスする方も
ListA_tp head; int headField = head->field0; int secondField = head->next->field0; int thirdField = head->next->next->field0;でOKです。
typedef struct Tree { struct Tree * lchild; /* まだ typedef は終わっていないので */ struct Tree * rchild; /* まだ typedef は終わっていないので */ int val; } Tree_t, * Tree_tp; ------ あるいは ------- typedef struct Tree Tree_t, * Tree_tp; /* 先に typedef しましょ */ struct Tree { Tree_tp lchild; Tree_tp rchild; int val; };でOKです。アクセスもこんな感じでおしまい。
Tree_tp root; int lchildVal = root->lchild->val; int rchildVal = root->rchild->val; int lchildOfrchildVal = root->rchild->lchild->val;
2001.11.19/ Tomio KAMADA: kamada@cs.kobe-u.ac.jp