// ex39 #include #include int main(void){ struct test{ char x; struct test *next; }; struct test A, B, C; /*A,B,C 3つのstruct test型変数に直接入力および接続*/ A.x = 'A'; A.next = &B; /*変数Aの次のアドレスは&B*/ B.x = 'B'; B.next = &C; /*変数Bの次のアドレスは&C*/ C.x = 'C'; C.next = NULL; /*変数Cの次のアドレスはNULL(終了)*/ printf("%c %c",A.x,(*(A.next)).x,); // こんな感じで構造体C.xを表示するにはどうすればよいのか // printf("%c %c",A.x,(A.next)->x, )でもよい; } **************************** // ex40 /*3つのstruct test型変数をアドレスとして確保する場合*/ #include #include int main(void){ struct test{ char x; struct test *next; }; struct test *A, *B, *C; A = (struct test *)malloc(sizeof(struct test)); B = (struct test *)malloc(sizeof(struct test)); C = (struct test *)malloc(sizeof(struct test)); /*A,B,C 3つのstruct test型変数に直接入力および接続*/ A->x = 'A'; A->next = ; /*変数Aの次のアドレスは&Bでいいのか?*/ B->x = 'B'; B->next = ; /*変数Bの次のアドレスは&Cでいいのか?*/ C->x = 'C'; C->next = NULL; /*変数Cの次のアドレスはNULL(終了)*/ printf("%c %c %c\n",A->x,,); } /* A, B, C を普通に宣言するのと,アドレス型で宣言するのとで どちらが簡単に扱えるか実感できましたか? -> 演算子はとても便利ですね. /* /********************************************************/ //ex41 #include #include int main(void){ struct test{ char x; struct test *next; }; struct test *first, *p, *q, *temp;; /*先頭部分を作ります.char x は'A'.*/ first = (struct test *)malloc(sizeof(struct test)); /*箱を作って*/ first->x = 'A'; first->next = NULL; p = first; while(p->next != NULL) p = p->next; /*pの次にqという箱を作って挿入します(char x は 'B')*/ q = (struct test *)malloc(sizeof(struct test)); /*箱を作って*/ q->x = 'B'; q->next = NULL; p->next = q; /*つなげる*/ p = first; while(p->next != NULL) p = p->next; /*pの次にqという箱を作って挿入します(char x は 'D')*/ q = (struct test *)malloc(sizeof(struct test)); /*箱を作って*/ q->x = 'D'; q->next = NULL; p->next = q; /*つなげる*/ /******************************************************* この時点で,first=Aの箱 --> Bの箱 --> Dの箱 --> NULL のようにつながっていることがわかりますか? ********************************************************/ /*以下は先頭からリスト内を出力をする標準的方法です.*/ p = first; while(p != NULL){ printf("%c",p->x); /* ABD と出力されます.*/ p = p->next; } printf("\n"); /*問41:BとDの間にCを挿入してみましょう*/ /*この下にプログラムを記述*/ /*以下は先頭からリスト内を出力をする標準的方法です.*/ p = first; while(p != NULL){ printf("%c",p->x); p = p->next; } printf("\n"); /* ABCDのように出力できましたか?*/ } /********************************************************/ //ex42 /*問42:AとCの間のBを削除してみよう*/ /*また,Bの入っていたメモリはfree()で解放してください.*/ #include #include int main(void){ struct test{ char x; struct test *next; }; struct test *first, *p, *q, *temp;; /*先頭部分を作ります.char x は'A'.*/ first = (struct test *)malloc(sizeof(struct test)); /*箱を作って*/ first->x = 'A'; first->next = NULL; p = first; while(p->next != NULL) p = p->next; /*pの次にqという箱を作って挿入します(char x は 'B')*/ q = (struct test *)malloc(sizeof(struct test)); /*箱を作って*/ q->x = 'B'; q->next = NULL; p->next = q; /*つなげる*/ p = first; while(p->next != NULL) p = p->next; /*pの次にqという箱を作って挿入します(char x は 'C')*/ q = (struct test *)malloc(sizeof(struct test)); /*箱を作って*/ q->x = 'C'; q->next = NULL; p->next = q; /*つなげる*/ p = first; while(p->next != NULL) p = p->next; /*pの次にqという箱を作って挿入します(char x は 'D')*/ q = (struct test *)malloc(sizeof(struct test)); /*箱を作って*/ q->x = 'D'; q->next = NULL; p->next = q; /*つなげる*/ /******************************************************* この時点で,first=Aの箱 --> Bの箱 -->Cの箱 --> Dの箱 --> NULL のようにつながっている ********************************************************/ /*以下は先頭からリスト内を出力をする標準的方法です.*/ p = first->next; while(p != NULL){ printf("%c",p->x); /* ABCD と出力されます.*/ p = p->next; } printf("\n"); /*問42:Bを消去してみよ*/ /*この下にプログラムを記述*/ p = first; while(p->next->x != 'B') p = p->next; temp = /*以下は先頭からリスト内を出力をする標準的方法です.*/ p = first; while(p != NULL){ printf("%c",p->x); p = p->next; } printf("\n"); /* ACDのように出力できましたか?*/ } /********************************************************/ //ex44 #include #include struct test{ char x; struct test *next; }; struct test *insert(char a, struct test *p, struct test *init); int main(void){ struct test *front,*p; front = insert('A',NULL,NULL); front = insert('B',front,front); /*何じゃこりゃ?でも関数insertがわかっていれば わかるはず*/ printf("%c\n",front->x); printf("%c\n",front->next->x); /* この後にDを挿入せよ */ /* front = insert('D',front,front); では間違い*/ /*関数insertはポインタpの指すセルの 次に aのセルを挿入,p=NULLならば先頭に挿入 ということに注意せよ*/ /* さらにデータ'C'を構造体BとDの間に挿入せよ */ p = front; while(/*適当に埋める*/){ /*適当に埋める*/ } front = /*適当に埋める*/ /* 先頭からデータの表示を行え */ return(0); } struct test *insert(char a, struct test *p, struct test *init){ /*ポインタpの指すセルの次にaのセルを挿入,p=NULLならば先頭に挿入*/ struct test *q, *r; r = (struct test *)malloc(sizeof(struct test)); if(p==NULL){ q = init; init = r; init->x = a; init->next = q; }else{ q = p->next; p->next = r; r->x = a; r->next = q; } return(init); } /********************************************************/ //ex45 #include #include struct test{ char x; struct test *next; }; struct test *insert(char a, struct test *p, struct test *init); struct test *delete(struct test *p, struct test *init); int main(void){ struct test *front,*p; front = insert('A',NULL,NULL); front = insert('B',front,front); /* 'C'と'D'の挿入を行え */ /* 先頭からデータの表示を行え */ /* (1)'C'を捜してdeleteを使って削除する */ /* (2)'A'を捜してdeleteを使って削除する */ p = front; while(/*適当に埋める*/!= 'C'){ /*適当に埋める*/ } front = /*適当に埋める*/ /* 先頭からデータの表示を行え */ return(0); } struct test *insert(char a, struct test *p, struct test *init){ /*ポインタpの指すセルの次にaのセルを挿入,p=NULLならば先頭に挿入*/ struct test *q, *r; r = (struct test *)malloc(sizeof(struct test)); if(p==NULL){ q = init; init = r; init->x = a; init->next = q; }else{ q = p->next; p->next = r; r->x = a; r->next = q; } return(init); } struct test *delete(struct test *p, struct test *init){ /* ポインタpが指すセルを除去 */ struct test *q; if(p == init){/*先頭を削除するかどうかの判定*/ q = p->next; free(p); return(q); } q = init; while(q->next != p){ q = q->next; } q->next = p->next; free(p); return(init); }