1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define NUM 4 5 6 struct chain 7 { 8 char name[20]; 9 char city[20]; 10 char sex[10]; 11 char age[10]; 12 char job[10]; 13 struct chain *next; 14 }; 15 16 struct chain *create(); 17 struct chain *SequelSeach(head, name); 18 void print_data(point); 19 20 struct chain Datas[NUM] = 21 { 22 "Sun", "Weifang", "Male", "24", "student", NULL, 23 "Tom", "Beijing", "Male", "31", "doctor", NULL, 24 "Marry", "Shanghai", "Female", "19", "techer", NULL, 25 "Willing", "Tianjing", "Female", "21", "worker", NULL 26 }; 27 28 int main() 29 { 30 struct chain *head; 31 struct chain *p; 32 char name[30]; 33 head = create(); 34 printf("请输入要查找的人名 "); 35 scanf("%s", name); 36 p = SequelSeach(head, name); 37 print_data(p); 38 return 0; 39 } 40 41 struct chain *create() 42 { 43 struct chain *head, *tail, *p; 44 int i; 45 head = tail = NULL; 46 printf("将名单数据输入到链表中: "); 47 for (i = 0; i < NUM; i++) 48 { 49 p = (struct chain *)malloc(sizeof (struct chain)); 50 strcpy(p->name, Datas[i].name); 51 strcpy(p->city, Datas[i].city); 52 strcpy(p->sex, Datas[i].sex); 53 strcpy(p->age, Datas[i].age); 54 strcpy(p->job, Datas[i].job); 55 p->next = NULL; 56 if (head == NULL) 57 head = tail = p; 58 else 59 tail = tail->next; 60 tail->next = p; 61 } 62 return head; 63 } 64 65 struct chain *SequelSeach(head, name) 66 struct chain *head; 67 char name[]; 68 { 69 struct chain *temp; 70 temp = head; 71 for (temp = head; temp != NULL;) 72 { 73 if (strcmp(temp->name, name) == 0) 74 break; 75 else 76 temp = temp->next; 77 } 78 if (temp == NULL) 79 printf("没有查找到该人资料 "); 80 return temp; 81 } 82 83 void print_data(point) 84 struct chain *point; 85 { 86 if (point == NULL) 87 return; 88 printf("查找结果: "); 89 printf(" 姓名:%s ", point->name); 90 printf(" 城市:%s ", point->city); 91 printf(" 性别:%s ", point->sex); 92 printf(" 年龄:%s ", point->age); 93 printf(" 工作:%s ", point->job); 94 }