Search code examples
cpointersdata-structureslinked-liststructure

Sorting numbers high to low in C Linked List


I would like to sort the indexes of the students that I added in the Main method according to the scores they get from high to low. This is the last form of the list that i want to create.

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

This is the code i wrote:

#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;
    }
}


Solution

  • You could insert the new student at the sorted location directly :

    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,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,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
    */