Memory usage problem when trying to sort a struct list in C

0

I am trying to sort a struct list in C and i get a 0xC0000005 problem. This is the struct i am using:

typedef struct Aqi Aqi;
struct Aqi{
    int date; // format: yyyymmdd (20200101) - 1st Jan 2020
    int time; // Either 9 , 12 or 19
    int l1; // l stands for location
    int l2;
    int l3;
    Aqi* next;
};

The list is presorted by date and there are three elements for each date (For each given time). So i am using this function to sort the list by times in ascending order:

Aqi* sort(Aqi* root){ // I am passing the root of the list
    int date;
    int tempInt;
    int c = 0;
    int min;

    Aqi* temp = root;
    Aqi* temp2 = NULL;

    while(temp != NULL){
        if(c == 3){ // Counter c is being used to restart values on every 3rd iteration because
            c = 0; // that's when the new date starts.
        }
        if(c == 0){
            date = temp->date;
            temp2 = temp;
            min = temp2->time;
        }
        while(temp2->date == date){
            if(temp2->time < min){
                min = temp2->time;

                tempInt = temp->time;
                temp1->time = min;
                temp3->time = tempInt;

                tempInt = temp->l1;
                temp->l1 = temp2->l1;
                temp2->l1 = tempInt;

                tempInt = temp->l2;
                temp->l2 = temp2->l2;
                temp2->l2 = tempInt;

                tempInt = temp->l3;
                temp->l3 = temp2->l3;
                temp2->l3 = tempInt;
            }

            temp2 = temp2->next;
        }
        ++c;

        temp = temp->next;
    }
    return root;
}

The list itself is fine (I can print it with no problem) but the program is having trouble with this function specifically. Why is it giving me an error?

c
list
structure
asked on Stack Overflow Jan 21, 2021 by MilanJ • edited Jan 21, 2021 by Ian Abbott

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0