微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将C链表中的数字从高到低排序

如何解决将C链表中的数字从高到低排序

我想根据从高到低的分数对在Main方法添加的学生的索引进行排序。这是我要创建的列表的最后一种形式。

0,18061086,65
1,20060032,60
2,20060678,55
3,20060045,50
4,19061091,45
5,18060311,40
6,20060134,30

这是我写的代码

#include<stdio.h>
#include<stdlib.h>

struct student{
    int index;
    int studentnumber;
    int score;
    struct student* next;
};

typedef struct student Student;
void addStudent(Student **headOfList,int index,int studentnumber,int score);
void score(Student* headOfList);
int main(){
    Student* headOfList = NULL;
    addStudent(&headOfList,40);
    addStudent(&headOfList,1,50);
    addStudent(&headOfList,2,45);
    addStudent(&headOfList,3,30);
    addStudent(&headOfList,4,55);
    addStudent(&headOfList,5,65);
    addStudent(&headOfList,6,60);
    return 0;
}


void addStudent(Student **headOfList,int score){
    Student* currentStudent = (*headOfList);
    Student* newStudent = (Student*)malloc(sizeof(Student));
    newStudent->index =index;
    newStudent->studentnumber = studentnumber;
    newStudent->score = score;
    newStudent->next = NULL;
    if(currentStudent == NULL){
        // if list is empty
        (*headOfList)= newStudent;
    }
    else{
        while(currentStudent->next != NULL){
            currentStudent = currentStudent->next;
        }   
        currentStudent -> next = newStudent;
    }
}

解决方法

您可以直接在排序后的位置插入新学生:

void display(Student* headOfList){ 
    Student* currentStudent= headOfList; 
    while(currentStudent!=NULL){ 
        printf("index no : %-3d student Number : %-6d student score : %-3d\n",currentStudent->index,currentStudent->studentnumber,currentStudent->score); 
        currentStudent = currentStudent->next; 
    } 
} 

void addStudent(Student **headOfList,int index,int studentnumber,int score) {
    Student* currentStudent = (*headOfList);
    Student* newStudent = (Student*)malloc(sizeof(Student));
    Student* prev = NULL;
    newStudent->index =index;
    newStudent->studentnumber = studentnumber;
    newStudent->score = score;
    newStudent->next = NULL;
    printf("====== Add new student :\n");
    if(currentStudent == NULL){
        // if list is empty
        (*headOfList)= newStudent;
    }
    else if (score >= (*headOfList)->score) {
        *headOfList = newStudent;
        newStudent->next = currentStudent;
    }
    else {
        while(currentStudent->next != NULL && currentStudent->next->score >= score) {
            currentStudent = currentStudent->next;
        }   
        //printf("%d %d \n",score,currentStudent->score);
        Student* next = currentStudent->next;
        currentStudent->next = newStudent;
        newStudent->next = next;
    }
    if (headOfList) {
        display(*headOfList);
    }
}

int main(){
    Student* headOfList = NULL;
    addStudent(&headOfList,18060311,40);
    addStudent(&headOfList,1,20060045,50);
    addStudent(&headOfList,2,19061091,45);
    addStudent(&headOfList,3,20060134,30);
    addStudent(&headOfList,4,20060678,55);
    addStudent(&headOfList,5,18061086,60);
    addStudent(&headOfList,6,20060032,60);
    
    printf("================ Final result :\n");
    display(headOfList);
    return 0;
}
/*
Program returned: 0
====== Add new student :
index no : 0   student Number : 18060311 student score : 40 
====== Add new student :
index no : 1   student Number : 20060045 student score : 50 
index no : 0   student Number : 18060311 student score : 40 
====== Add new student :
index no : 1   student Number : 20060045 student score : 50 
index no : 2   student Number : 19061091 student score : 45 
index no : 0   student Number : 18060311 student score : 40 
====== Add new student :
index no : 1   student Number : 20060045 student score : 50 
index no : 2   student Number : 19061091 student score : 45 
index no : 0   student Number : 18060311 student score : 40 
index no : 3   student Number : 20060134 student score : 30 
====== Add new student :
index no : 4   student Number : 20060678 student score : 55 
index no : 1   student Number : 20060045 student score : 50 
index no : 2   student Number : 19061091 student score : 45 
index no : 0   student Number : 18060311 student score : 40 
index no : 3   student Number : 20060134 student score : 30 
====== Add new student :
index no : 5   student Number : 18061086 student score : 60 
index no : 4   student Number : 20060678 student score : 55 
index no : 1   student Number : 20060045 student score : 50 
index no : 2   student Number : 19061091 student score : 45 
index no : 0   student Number : 18060311 student score : 40 
index no : 3   student Number : 20060134 student score : 30 
====== Add new student :
index no : 6   student Number : 20060032 student score : 60 
index no : 5   student Number : 18061086 student score : 60 
index no : 4   student Number : 20060678 student score : 55 
index no : 1   student Number : 20060045 student score : 50 
index no : 2   student Number : 19061091 student score : 45 
index no : 0   student Number : 18060311 student score : 40 
index no : 3   student Number : 20060134 student score : 30 
================ Final result :
index no : 6   student Number : 20060032 student score : 60 
index no : 5   student Number : 18061086 student score : 60 
index no : 4   student Number : 20060678 student score : 55 
index no : 1   student Number : 20060045 student score : 50 
index no : 2   student Number : 19061091 student score : 45 
index no : 0   student Number : 18060311 student score : 40 
index no : 3   student Number : 20060134 student score : 30
*/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。