1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct node { char id[20]; char name[40]; char sex; int old; int grade; char address[40]; struct node *next; } Node; void addNode(Node **head, char id[], char name[], char sex, int old, int grade, char address[]) { Node *p = (Node*)malloc(sizeof(Node)), *q = *head; strcpy((*p).id, id); strcpy((*p).name, name); (*p).sex = sex; strcpy((*p).address, address); (*p).old = old; (*p).grade = grade; if(*head != 0) { *head = p; (*p).next = q; } else { *head = p; (**head).next = NULL; } } void readNode(Node **head) { Node *last = *head; while(last != 0) { printf("%s %s %c %d %d %s\n", last->id, last->name, last->sex, last->old, last->grade, last->address); last = last->next; } } int main() { Node *head = NULL; static char id[20], name[40], sex, address[40], *p=id; static int old, grade; while(scanf("%s", id), strcmp(id, "end")) { scanf("%s ", name); scanf("%c", &sex); scanf("%d", &old); scanf("%d", &grade); scanf("%s", address); addNode(&head, id, name, sex, old, grade, address); for(p = id; *p != 0; p++) { *p = 0; } } readNode(&head); }
|