Why does the sequential sort not work? (error code -1073741819 (0xC0000005) )

-1

My Fundamentals of Computer Science professor tasked me to bring to him a fully functional program extracted from one of his written exams. In this case I had to bring a sequential sort on a linked list with pointers (I think that's how it's called in English), and I've been working for now several days on this program, but it doesn't want to work.
Apparently as the error code suggests that is due to a poor usage of pointers, but me and my cousin tried to change them multiple times, with varying degrees of failure.
Don't worry about the printf texts in Italian, they only serve to, well, print out the results. This program either stops before the seq_sort with the error code that is in the title Or, if we replace the double pointers in the seq_sort with single pointers, just prints the first two elements of the list ad libitum, unless of course you stop it during runtime. Any help is really appreciated because I need to present this to my professor as soon as possible and I can't figure out where the problem lies. I can ask for a delay in case but the sooner we solve this issue the better. I've also read that the problem might be Windows as well, as we were trying to do it on my cousin's laptop (I'm currently configuring CLion on my laptop with Xubuntu in dual boot with Win 10), in case it might be useful as a detail.

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

typedef unsigned short int boolean;
#define TRUE 1
#define FALSE 0

struct list {
    float value;
    struct list *next;
};

boolean isGreater (float a, float b);
void init(struct list **ptr);
void seq_sort (struct list **ptr, int length);
void visit(struct list *ptr, int l);
void del_list(struct list **ptrptr);
void pre_insert(struct list **ptrptr,float value);
void swap(struct list **ptrptr, int a, int b);

int main() {
    struct list *ptr;
    float value;
    int a, b;
    init(&ptr);
    printf("Inserisci il numero di elementi della lista: ");
    scanf("%d", &b);
    for(a=0; a<b; a++){
        printf("Inserisci valore: ");
        scanf("%f", &value);
        pre_insert(&ptr, value);
    }
    printf("Eseguo Sequential Sort. \n");
    seq_sort(&ptr,b);
    printf("Stampo la lista. \n");
    visit(ptr, b);
    del_list(&ptr);
}

void init(struct list **ptr){
    *ptr=NULL;
}

void visit(struct list *ptr, int l){
    int count = 0;
    while(ptr != NULL && count < l){
        printf("%f", ptr->value);
        printf("\n");
        ptr = ptr->next;
        count ++;
    }
}

void del_list(struct list **ptrptr){
    struct list *tmp_ptr;
    while(*ptrptr!=NULL){
        tmp_ptr=*ptrptr;
        *ptrptr=(*ptrptr)->next;
        free(tmp_ptr);
    }
    printf("lista cancellata \n");
}

void pre_insert(struct list **ptrptr,float value){
   struct list *tmp_ptr;
   tmp_ptr=*ptrptr;
   *ptrptr=(struct list *)malloc(sizeof(struct list));
    if(*ptrptr==NULL)
        printf("errore allocazione memoria, riprovare");
    else {
         (*ptrptr)->value = value;
        (*ptrptr)->next = tmp_ptr;
    }
}

void swap(struct list **ptrptr, int a, int b){
    struct list **tmp_ptr;
    struct list **tmp;
    float tmp_value;
    tmp=ptrptr;
    int count=0, i=0;
    while(count<a){
        ptrptr=&((*ptrptr)->next);
        count++;
    }
    tmp_ptr=ptrptr;
    ptrptr=tmp;
    while(i<b){
        ptrptr=&((*ptrptr)->next);
        i++;
    }
    tmp_value=(*ptrptr)->value;
    (*ptrptr)->value=(*tmp_ptr)->value;
    (*tmp_ptr)->value=tmp_value;
}

boolean isGreater (float a, float b){
    if (b-a>FLT_EPSILON)
        return TRUE;
    else
        return FALSE;
}

void seq_sort (struct list **ptr, int length){
    int count = 0, a;
    float max;
    struct list **tmp;
    tmp = ptr;
    for(int iter = 0;iter<length-1;iter++){
        a = 0;
        max = (*ptr)->value;
        ptr = &((*ptr)->next);
        count = 1;
        while(*ptr != NULL && count < length - iter) {
            if (isGreater((*ptr)->value, max)) {
                max = (*ptr)->value;
                a = count;
            }
            count++;
            ptr = &((*ptr)->next);
        }
        ptr = tmp;
        if(a != length - iter)
            swap(ptr, a, length - iter);
    }
}
c
sorting
sequential
asked on Stack Overflow Oct 11, 2019 by Levi Bonaparte • edited Oct 11, 2019 by Levi Bonaparte

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0