#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { HEAPSIZE = 100, NAMESIZE = 25, EMAILSIZE = 30, AGESIZE = 16, };
struct node {
char name[NAMESIZE]; /* 氏名*/
char email[EMAILSIZE]; /* E-mail アドレス*/
int age; /* 年齢*/
struct node *next;
} heap[HEAPSIZE], *hp = heap;
struct node *new(char *name, char *email, int age){
strncpy(hp->name, name, sizeof(hp->name) - 1);
strncpy(hp->email, email, sizeof(hp->email) - 1);
hp->age = age;
hp->next = NULL;
return hp++;
}
struct node *add(struct node *p,struct node *q){
q->next = p;
p = q;
return p;
}
void printf_list(struct node *p){
while(p != NULL){
printf("Name: %s\n", p->name);
printf("E-mail: %s\n", p->email);
printf("Age: %d\n", p->age);
p = p->next;
}
printf("\n");
}
void getline(char *s, int n){
int c;
while (--n > 0 && ((c = getchar()) != EOF && c != '\n'))
*s++ = c;
*s = '\0';
}
int main(){
struct node *root = NULL;
char name_buf[NAMESIZE] = { '\0' };
char email_buf[EMAILSIZE] = { '\0' };
char age_buf[AGESIZE] = { '\0' };
int age = -1, n = 0;
for(n = 0; n < HEAPSIZE; n++) {
memset(name_buf, 0, sizeof(name_buf));
memset(email_buf, 0, sizeof(email_buf));
memset(age_buf, 0, sizeof(age_buf));
getline(age_buf, sizeof(age_buf));
if (strlen(age_buf) == 2 && atoi(age_buf) == -1) {
break;
}
age = atoi(age_buf);
getline(name_buf, sizeof(name_buf));
getline(email_buf, sizeof(email_buf));
root = add(root, new(name_buf, email_buf, age));
}
printf_list(root);
return 0;
}