#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct NAME {
char *name;
struct NAME *next;
};
int putlist(struct NAME **first, char *name) {
char *p;
struct NAME *node;
if (!(p = malloc(strlen(name) + 1)))
return 0;
strcpy(p, name);
if (!(node = malloc(sizeof(struct NAME)))) {
free(p);
return 0;
}
node->name = p;
node->next = *first;
*first = node;
return 0;
}
char *getlist(struct NAME **first) {
char *p;
struct NAME *node;
if (!*first)
return NULL;
node = *first;
p = node->name;
*first = node->next;
free(node);
return p;
}
int main() {
struct NAME *first;
char *p;
first = NULL;
putlist(&first, "山田");
putlist(&first, "田中");
putlist(&first, "佐藤");
putlist(&first, "鈴木");
putlist(&first, "村上");
putlist(&first, "山本");
putlist(&first, "裏山");
putlist(&first, "田山");
putlist(&first, "小島");
putlist(&first, "島々");
while (p = getlist(&first)) {
printf("%s\n", p);
free(p);
}
return 0;
}
/* end */