我想根据他们获得的分数从高到低对我在 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,0,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,65);
addStudent(&headOfList,6,20060032,60);
return 0;
}
void addStudent(Student **headOfList,int index,int studentnumber,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;
}
}