- 452 名前:デフォルトの名無しさん mailto:sage [2009/07/02(木) 16:17:23 ]
- >>443 無理やり1レスでスマソ
/* 課題2 */ #include <stdio.h> #include <stdlib.h> struct Node { int value; struct Node *next; }; struct Node *init_list() { struct Node *root = (struct Node *)malloc(sizeof(struct Node)); root->value = 0; root->next = NULL; return root; } void add_node(struct Node *root, int value) { struct Node *p = root->next, *q = root; while (p) { q = p; p = p->next; } p = (struct Node *)malloc(sizeof(struct Node)); p->value = value; p->next = NULL; q->next = p; } void make_list(struct Node *root) { int value; printf("Insert:\n"); while (scanf("%d", &value) != EOF) { add_node(root, value); } } void show_list(struct Node *root) { struct Node *p = root->next; printf("Show:\n"); while (p) { printf("%d\n", p->value); p = p->next; } } void free_list(struct Node *root) { if (root) { free_list(root->next); free(root); } } int main() { struct Node *root = init_list(); make_list(root); show_list(root); free_list(root); return 0; }
|

|