⑴输入10个职工的姓名和职工号;⑵按职工号由小到大排序,姓名顺序也随之调整;⑶要求输入一个职工号,查找法找出该职工的姓名。从主函数输入要查找的职工号,输出该职工姓名

来源:互联网 发布:linux 漏洞扫描软件 编辑:程序博客网 时间:2024/05/16 18:32
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 
  5 #define len sizeof(struct student)
  6 #define n 3
  7 
  8 struct student
  9 {
 10     char name[20];
 11     int num;
 12     struct student *next;
 13 };
 14 
 15 typedef struct student stu;
 16 stu *input(stu *head);
 17 stu *sort(stu *head);
 18 stu *find(stu *head,int m);
 19 void print(stu *head);
 20 
 21 int main()
 22 {
 23     stu *head, *q;
 24     int m;
 25 
 26     printf("input stu 's name and num\n");
 27     head=input(head);
 28     print(head);
 29 
 30 
 31     head=sort(head);
 32     print(head);
  31     head=sort(head);
 32     print(head);
 33 
 34     printf("input tao find de shu\n");
 35     scanf("%d",&m);
 36     q=find(head, m);
 37     printf("%s %d\n",q->name,q->num);
 38 
 39     return 0;
 40 }
 41 
 42 stu *input(stu *head)
 43 {
 44     stu *p, *tail;
 45     int count=0;
 46     while((count<=n)&&(tail!=NULL))
 47     {
 48         tail=(stu *)malloc(len);
 49         scanf("%s %d",&tail->name,&tail->num);
 50             count++;
 51         if(count==1)
 52         {
 53             head=tail;
 54             p=tail;
 55         }
 56         else
 57         {
 58             p->next=tail;
 59             p=tail;
 60         }
 61         if(count==n)
 62         {
  61         if(count==n)
 62         {
 63             tail=NULL;
 64         }
 65     }
 66 
 67     return head;
 68 }
 69 
 70 stu *sort(stu *head)
 71 {
 72     char a[20];
 73     stu *p;
 74     int temp, i, j;
 75 
 76     p=head;
 77     for(j=0; j<n-1; j++)
 78     {   p=head;
 79         for(i=0; i<n-j-1; i++)
 80         {
 81             if(p->num>p->next->num)
 82             {
 83                 temp=p->num;
 84                 p->num=p->next->num;
 85                 p->next->num=temp;
 86                 strcpy(a,p->name);
 87                 strcpy(p->name,p->next->name);
 88                 strcpy(p->next->name,a);
 89             }
 90             p=p->next;
 91         }
 92     }
  91         }
 92     }
 93 
 94     return head;
 95 }
 96 
 97 stu *find(stu *head,int m)
 98 {
 99     stu *p, *q;
100 
101     p=head;
102     while(p!=NULL)
103     {
104         if(p->num==m)
105         {
106             q=p;
107             break;
108         }
109         p=p->next;
110     }
111     return q;
112 }
113 
114 void print(stu *head)
115 {
116     stu *p;
117     p=head;
118     while(p!=NULL)
119     {
120         printf("%s %d\n",p->name,p->num);
121         p=p->next;
122     }
123 }
~                                                                                                                         91,3-9        97%
                                                                                                                      61,3-9        65%
                                                                                                                      31,2-5        32%
                                                                                                                      1,5          顶端
0 0
原创粉丝点击