/* * linklist裡面還有linklist * 只是內容上會反過來 * 例如: 1,2,3-->3,2,1 * 以下的範例會用兩個for回圈產生不固定長度的父和子 */ #include #include #include //子節點 typedef struct list_child { int val; struct list_child* next; } listC; //父節點 typedef struct list_parent { struct list_child *c; struct list_parent *next; } listP; int main() { listC *curC, *headC; listP *curP, *headP; int i,j; srand(time(NULL)); //增加父節點 headP= NULL; for(i=0;ival = j; curC->next = headC; headC=curC; } printf("j=%d\n", j-1); curP->c=headC; //把子節點加入父節點 curP->next=headP; headP=curP; } //end of 增加父節點 printf("i=%d\n", i-1); curP=headP; //把cur重新指回起點 i=0; while(curP) { printf("i=%d-->", i++); curC=curP->c; while(curC) { //訪問子節點 printf("%d,", curC->val); curC = curC->next; } printf("\n"); curP=curP->next; } return 0; }