链表排序_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();}
阅读全文
0 0
- 链表排序_2.0版
- 关于链表_2
- 排序_2.快速排序
- 数据结构排序问题_2
- 交换排序_2.快速排序
- 排序算法_2,快速排序
- 【CCF】201503_2数字排序
- 插入排序_2.折半插入排序
- WF4.0_2
- Win32 SEH异常深度探索_2 异常链表遍历
- Win32 SEH异常深度探索_2 异常链表遍历
- Win32 SEH异常深度探索_2 异常链表遍历
- Java飞机大战0_2
- Android_1.5_1.6_2.0_2.1_2.2区别
- 算法导论 思考题7_2 对区间的模糊排序
- 算法设计和数据结构学习_2(常见排序算法思想)
- C语言强化(七)链表相交问题_2 找到无环链表相交结点
- 写在MyUML发布090515_2版之际
- Python 批量处理文件
- 使用自签名SSL证书配置HTTPS,解决浏览器提示不安全警告
- 013 Python语法之文件与生成器
- 统计bit 1的个数
- 在QT中,当BOM头不存在的时候,判断一个文件是否为UTF-8编码格式
- 链表排序_2.0版
- 283. Move Zeroes(C++/Java)
- loadrunner11报错:Error -27780
- HDU 2063 Investment (完全背包+数据压缩)
- SQL的四种连接-左外连接、右外连接、内连接、全连接
- 安卓SwipeRefreshLayout的使用方法
- hdu-2089-不要62
- 什么是JavaBean,POJO
- HDU