取りあえず遅れている人も多い7週目ぐらいから解説していきましょうか。
7-1に関しては簡単で、説明の図に書いてあるのをそのまま打ち込めばいいのですね。
基本の形は
top->key
top->next->key
という風に、次の構造体をどんどん指して値を代入していく形です。
一応少し細かく書くと
top->key|next->key|next->
といった感じでしょうか。
#include
#include
#define MAX 100
//data型の構造体をまずは定義
struct data{
char key;
struct data *next;
};
int main(void){
struct data *top = NULL;
//aを代入する作業
top = (struct data*)malloc(sizeof(struct data)*1);
if(top == NULL) {
printf("メモリの確保に失敗しました。\n");
exit(EXIT_FAILURE);
}
top->key = 'a';
top->next = NULL;
//bを代入
top->next = (struct data*)malloc(sizeof(struct data)*1);
if(top == NULL) {
printf("メモリの確保に失敗しました。\n");
exit(EXIT_FAILURE);
}
top->next->key ='b';/*問題のようにnextが次のtopを指している*/
top->next->next = NULL;
//cを代入
top->next->next = (struct data*)malloc(sizeof(struct data)*1);
if(top == NULL) {
printf("メモリの確保に失敗しました。\n");
exit(EXIT_FAILURE);
}
top->next->next->key = 'c';
top->next->next->next = NULL;
printf("%c\n",top->key);
printf("%c\n",top->next->key);
printf("%c\n",top->next->next->key);
return 0;
}
