链表实现投票系统


  1     #include "stdio.h"
  2     #include "stdlib.h"
  3     #include "string.h"
  4     typedef struct Candidates{//创建候选人结构
  5         char name[20];//投票人姓名
  6         int vote;//投票数
  7         struct Candidates * next;
  8     }Candidate;
  9     typedef struct Student{//创建学生结构
 10         int number;//定义学号
 11         char name[20];
 12         char vote_Name[3][20];
 13         struct Student *next;
 14     }Stu;
 15     void print(Candidate *head)
 16     {
 17         Candidate *q = head;
 18         Candidate *q1 = head;
 19         int value = 0;
 20         int cnt = 1;//记录前三个人的票数
 21         while(q){//从大到小打印候选人的票情况
 22             q1 = q;
 23             if(cnt <= 3){
 24                 printf("%s %d\n",q->name,q->vote);
 25                 value = q->vote;
 26             }
 27             if(cnt > 3 && q->vote == value)//打印相同的票数
 28                 printf("%s %d\n",q->name,q->vote);
 29             cnt++;
 30             q = q->next;
 31         }
 32     }
 33     void compute(Stu *head1,Candidate *head2)
 34     {
 35         Stu *s1 = head1;//继承学生链表头节点
 36         Candidate *s2 = head2;//继承候选人链表头节点
 37         while(s2){
 38             s1 = head1;
 39             while(s1){//遍历学生链表
 40                 if(strcmp(s2->name,s1->vote_Name[0]) == 0 && strlen(s2->name) == strlen(s1->vote_Name[0]))
 41                     s2->vote++;//匹配上了就票数+1
 42                 if(strcmp(s2->name,s1->vote_Name[1]) == 0 && strlen(s2->name) == strlen(s1->vote_Name[1]))
 43                     s2->vote++;//匹配上了就票数+1
 44                 if(strcmp(s2->name,s1->vote_Name[2]) == 0 && strlen(s2->name) == strlen(s1->vote_Name[2]))
 45                     s2->vote++;//匹配上了就票数+1
 46                 s1 = s1->next;
 47             }
 48             s2 = s2->next;
 49         }
 50     }
 51     Candidate *init_Candidate(Candidate *Candidate_head)
 52     {
 53         int n;
 54         printf("Please input number of candidates:");
 55         scanf("%d",&n);
 56         for(int i = 0;i < n;i++){//输入n个候选人(调试成功)
 57             printf("Please input %d candidates name:",i+1);
 58             Candidate *Candidate1 = (Candidate *)malloc(sizeof(Candidate));
 59             Candidate1->vote = 0;
 60             scanf("%s",Candidate1->name);
 61             Candidate1->next = Candidate_head;//头插法创建链表
 62             Candidate_head = Candidate1;
 63         }
 64         return Candidate_head;
 65     }
 66     Stu * init_Student(Stu *Student_head)
 67     {
 68         int n;
 69         printf("Please input students num:");
 70         scanf("%d",&n);
 71         printf("Please input students number,name,votename(Three)\n");
 72         for(int i = 0;i < n;i++){//输入n个投票学生(调试成功)
 73             printf("Please input %d voter information:",i+1);
 74             Stu *student1 = (Stu *)malloc(sizeof(Stu));
 75             scanf("%d%s%s%s%s",&student1->number,student1->name,student1->vote_Name[0],student1->vote_Name[1],student1->vote_Name[2]);
 76             student1->next = Student_head;//头插法创建链表
 77             Student_head = student1;
 78         }
 79         return Student_head;
 80     }
 81     Candidate *InsertSort(Candidate *head)
 82     {
 83         Candidate *p1,*t,*p2,*t1;
 84         if(head == NULL || head->next == NULL)//如果无节点或者只有一个节点,则不需要排序
 85             return head;
 86         p1 = head->next;
 87         head->next = NULL;
 88         while(p1)
 89         {
 90             for(p2 = p1,t = head;t != NULL && t->vote > p2->vote;t1 = t,t = t->next);//p2用于定位要插入的节点 p1用于寻找下一个要插入的节点
 91             p1 = p1->next;
 92             if(t == head)//判断是否在头节点之前,是则更新头部
 93                 head = p2;
 94             else//插入在中间或尾部
 95                 t1->next = p2;
 96             p2->next = t;
 97         }
 98         return head;
 99     }
100     int main()
101     {
102         int n;
103         Candidate *Candidate_head = NULL;//定义候选人列表头节点
104         Stu *Student_head = NULL;//定义学生链表头
105         Candidate_head = init_Candidate(Candidate_head);//初始化候选人链表
106         Student_head = init_Student(Student_head);//初始化学生链表
107         compute(Student_head,Candidate_head);//统计候选人票数
108         Candidate_head = InsertSort(Candidate_head);
109         Candidate *q = Candidate_head;
110         print(Candidate_head);//打印候选人的票最多的前3位
111         return 0;
112     }

相关