I'm almost done with my assignment, the last part is to write a function that sorts a singly linked list via insertion sort.. I'm also bound by my assignment's pre-defined structs and typedefs:
struct le {
int value;
struct le *next;
};
typedef struct le listenelement;
typedef listenelement *list;
Those I can't change. The insertion sort function has to work with these parameters. If the parameter m is negative, the list is supposed to be sorted in descending order and otherwise in ascending order.
void sort(int m, list *l);
EDIT:
Here's my attempt to implement the answers from here.. I still can't get it to work. I tried creating a new list that is the final result called "asc" (for ascendingly sorted list) and an auxiliarly list "aux" but I'm stuck..
void sort(int m, list *l){
if ((m == 0) || (*l == NULL)) {
printf("Error.\n");
} else {
if (m>0) {
listenelement head = {0,NULL};
list asc = {&head};
list aux = *l;
while (aux != NULL) {
int val = aux->value;
delete_elem(val,&aux);
while ((asc->next != NULL) && ((asc->value)<val)) {
printf("%d\n", (asc->value));
asc=asc->next;
}
insert(val,&asc);
asc = &head;
}
*l = &head;
}
}
}