// // 第10回課題:リストの一括消去 // #include #include struct tfield { int data; struct tfield *pt; }; // tfield構造体の型名を「NODE型」に呼び替える typedef struct tfield NODE; NODE *talloc(void); void GenerateList(NODE **); void DisplayList(NODE *); void DestroyList(NODE **); void main(void) { NODE *head, *p, *old; GenerateList(&head); DisplayList(head); DestroyList(&head); DisplayList(head); } void GenerateList(NODE **head) // 逆順リスト生成 { NODE *p; int data; *head = NULL; while (printf("\nData: "),scanf("%d", &data)!=EOF){ p = talloc(); p->data = data; p->pt = *head; *head = p; } printf("\n"); } void DisplayList(NODE *head) // リスト一覧表示 { NODE *p; printf("\n"); if (head == NULL) { printf("List is empty.\n"); return; } p = head; while (p != NULL) { printf("->%d", p->data); p = p->pt; } printf("\n\n"); } void DestroyList(NODE **head) // リスト消去 { NODE *p, *old; // // (ここを作成する) // } NODE *talloc(void) // ノード新規作成 { NODE *p; p = (NODE *)malloc(sizeof(NODE)); if (p == NULL) { fprintf(stderr, "Cannot allocate memory for node.\n"); exit(1); } return p; }