用C语言链表实现学生成绩的冒泡,插入,选择排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct STUDENT{
char name[10];
int age;
int score;
char num[10];
}stu_data;
typedef struct STU_NODE{
stu_data data;
struct STU_NODE *stu_node_next;
}stu_node;
stu_node *create_stu_linked_list(){
stu_node *head = malloc(sizeof(stu_node));
stu_node *move = NULL;
int stu_num = 0;
printf("请输入你要创建的学生人数:");
scanf("%d",&stu_num);
for (int i = 0; i < stu_num; i++)
{
if (0 == i)
{
move = malloc(sizeof(stu_node));
head->stu_node_next = move;
move->stu_node_next = NULL;
continue;
}
move->stu_node_next = malloc(sizeof(stu_node));
move = move->stu_node_next;
move->stu_node_next = NULL;
}
return head;
}
void input_stu_data(stu_node *head){
stu_node *move = head->stu_node_next;
int i = 1;
while (move != NULL)
{
printf("请输入第%d个学生的姓名 年龄 成绩 学号:",i);
scanf("%s %d %d %s",move->data.name,&move->data.age,&move->data.score,move->data.num);
move = move->stu_node_next;
i++;
}
}
void out_stu_data(stu_node *head){
stu_node *move = head->stu_node_next;
int i = 1;
while(move != NULL){
printf("第%d个学生的姓名:%s, 年龄:%d, 成绩:%d, 学号:%s.\n",i,move->data.name,move->data.age,move->data.score,move->data.num);
move = move->stu_node_next;
i++;
}
}
void insert_node(stu_node *head){
stu_node *move = head->stu_node_next;
printf("请输入你要插入在此学生后的学生学号:");
char pre_stu_num[10] = "\0";
scanf("%s",pre_stu_num);
while(NULL != move){
if (0 == strcmp(move->data.num,pre_stu_num))
{
stu_node *fresh = malloc(sizeof(*fresh));
printf("请输入你要插入的学生的姓名 年龄 成绩 学号:");
scanf("%s %d %d %s",fresh->data.name,&fresh->data.age,&fresh->data.score,fresh->data.num);
fresh->stu_node_next = move->stu_node_next;
move->stu_node_next = fresh;
return;
}
move = move->stu_node_next;
}
printf("未找到插入该学生后的学生学号,插入失败!\n");
}
void delete_node(stu_node *head){
stu_node *move = head;
printf("请输入你要删除的的学生学号:");
char del_stu_num[10] = "\0";
scanf("%s",del_stu_num);
while(NULL != move->stu_node_next){
if (0 == strcmp(move->stu_node_next->data.num,del_stu_num));
{
move = move->stu_node_next->stu_node_next;
free(move->stu_node_next);
move->stu_node_next = NULL;
return;
}
move = move->stu_node_next;
}
printf("未找到要删除的学生学号,删除失败!\n");
}
void bubble_sort(stu_node *head){
stu_node *move = head->stu_node_next;
stu_node *turn = head->stu_node_next;
while(NULL != turn->stu_node_next){
while(NULL != move->stu_node_next){
if (move->data.score < move->stu_node_next->data.score)
{
stu_data temp = move->data;
move->data = move->stu_node_next->data;
move->stu_node_next->data = temp;
}
move = move->stu_node_next;
}
move = head->stu_node_next;
turn = turn->stu_node_next;
}
}
void insert_sort(stu_node *head){
stu_node *disorder_move =head->stu_node_next->stu_node_next;
stu_node *order_move = head;
stu_node *front = head->stu_node_next;
stu_node *back = NULL;
while (NULL != disorder_move)
{
while (order_move->stu_node_next != disorder_move)
{
if (order_move->stu_node_next->data.score < disorder_move->data.score)
{
back = disorder_move->stu_node_next;
disorder_move->stu_node_next = order_move->stu_node_next;
order_move->stu_node_next = disorder_move;
front->stu_node_next = back;
disorder_move = front;
break;
}
order_move = order_move->stu_node_next;
}
front = disorder_move;
disorder_move = disorder_move->stu_node_next;
order_move = head;
}
}
void select_sort(stu_node *head){
stu_node *max = head->stu_node_next;
stu_node *move = head->stu_node_next->stu_node_next;
stu_node *turn = head->stu_node_next;
stu_data temp;
while (NULL != turn->stu_node_next)
{
while (NULL != move)
{
if (max->data.score < move->data.score)
{
max = move;
}
move = move->stu_node_next;
}
if (turn != max)
{
temp = turn->data;
turn->data = max->data;
max->data = temp;
}
turn = turn->stu_node_next;
max = turn;
move = turn->stu_node_next;
}
}
void main(){
stu_node *head = create_stu_linked_list();
input_stu_data(head);
printf("排序前*********************************************\n");
out_stu_data(head);
select_sort(head);
printf("排序后*********************************************\n");
out_stu_data(head);
}