链表排序_2.0版

来源:互联网 发布:网络攻防比赛 编辑:程序博客网 时间:2024/06/07 09:41
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>//结构体声明struct st{    int ID;    char name[10];    int score[4];    struct st* next;};//候选名字char *name[5] = {    "刘德华",    "张学友",    "郭富城",    "黎明",    "石洛"};//打印格式char *print_model[] ={    "\n姓名:\t学号:\t语文:\t数学:\t英语:\t理综:\t\n",    "\n%s\t%d\t%d\t%d\t%d\t%d\t\n",    "\n姓名:\t学号:\n",    "\n%s\t%d\n",    "\n姓名:\t语文:\n",    "\n%s\t%d\n",    "\n姓名:\t数学:\n",    "\n%s\t%d\n",    "\n姓名:\t英语:\t\n",    "\n%s\t%d\n",    "\n姓名:\t理综:\t\n",    "\n%s\t%d\n",   };//创建链表struct st * list_build(struct st **arr, struct st* head){    int i,j;    for (i = 0; i < 5; i++)    {        arr[i] = (struct st*)malloc(sizeof(struct st));        strcpy(arr[i]->name, name[i]);        arr[i]->ID = rand() % 100 + 10;        for (j = 0; j<4; j++)        {            arr[i]->score[j] = rand() % 100 + 50;        }        if (i>0)        {            arr[i - 1]->next = arr[i];        }    }    head = arr[0];    arr[i - 1]->next = NULL;    return head;}//打印链表void list_print(struct st* head, struct st* p, int n){    printf(print_model[2*n]);    while (head != NULL)    {        switch(n)        {        case 0:            printf(print_model[2 * n + 1], head->name, head->ID, head->score[0], head->score[1], head->score[2], head->score[3]);                break;        case 1:            printf(print_model[2 * n + 1], head->name, head->ID);                break;        case 2:            printf(print_model[2 * n + 1], head->name, head->score[0]);                break;        case 3:            printf(print_model[2 * n + 1], head->name, head->score[1]);                break;        case 4:            printf(print_model[2 * n + 1], head->name, head->score[2]);                break;        case 5:            printf(print_model[2*n + 1], head->name, head->score[3]);                break;        }        head = head->next;    }    head = p;}//链表排序void  list_sort(struct st* head, struct st* p,int n){    struct st *start = NULL, *temp = NULL, *temp1, *temp2;    int choice_1, choice_2;    temp = (struct st *)malloc(sizeof(struct st));    while (head != NULL)    {        start = head;        while (start->next != NULL)        {            switch (n)            {            case 1:                choice_1 = head->ID;                choice_2 = start->next->ID;                break;            case 2:                choice_1 = head->score[0];                choice_2 = start->next->score[0];                break;            case 3:                choice_1 = head->score[1];                choice_2 = start->next->score[1];                break;            case 4:                choice_1 = head->score[2];                choice_2 = start->next->score[2];                break;            case 5:                choice_1 = head->score[3];                choice_2 = start->next->score[3];                break;            }            //交换结构体,并重置对应的指针            if (choice_1 < choice_2)            {                temp1 = head->next;          //交换前将双方的结构体指针的指向保存                temp2 = start->next->next;                *temp = *(start->next);     //交换结构体所有元素的值                *(start->next) = *head;                *head = *temp;                            head->next = temp1;        //恢复为原有指向                start->next->next = temp2;            }            start = start->next;        }        head = head->next;    }    head = p;               //头指针复位    list_print(head, p, n);    //显示排序后的效果}void main(){    int n;    struct st *arr[5], *head = NULL;    head = list_build(arr, head);  //生成链表    list_print(head, arr[0], 0);   //显示所有学生信息    printf("\n请选择排序方式:1,按ID\t2,按语文成绩\t3,按数学成绩\t4按英语成绩\t5,按理综成绩\t\n");    scanf("%d", &n);    getchar();    list_sort(head, arr[0],n);    //按指定方式排序    getchar();}

这里写图片描述

这里写图片描述