存放个人信息到在结构体中 利用姓名查找他的其他信息

 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("请输入要查找的人名\n");
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("将名单数据输入到链表中:\n");
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("没有查找到该人资料\n");
80     return temp;
81 }
82 
83 void print_data(point)
84 struct chain *point;
85 {
86     if (point == NULL)
87         return;
88     printf("查找结果:\n");
89     printf("    姓名:%s\n", point->name);
90     printf("    城市:%s\n", point->city);
91     printf("    性别:%s\n", point->sex);
92     printf("    年龄:%s\n", point->age);
93     printf("    工作:%s\n", point->job);
94 }

 

posted @ 2019-10-16 22:01  insist钢  阅读(897)  评论(0编辑  收藏  举报