I'd like to implement a sorting algorithm(quicksort or bubblesort or mergesort... it doesn't matter. anything that sorts is ok) into my insert function, so that everytime i create a new element it'll be inserted into right position. The variable listptr is the pointer pointing to the address of the first element of the list, val is value according to which the sorting should be done and next is pointer pointing to position of next element of the list.
Could someone help me with this???
liste_t* Insert (liste_t *listptr, int val) {
liste_t *lp = listptr;
if (listptr != 0) {
while (listptr->next != 0){
listptr = listptr->next;
}
listptr->next = (struct liste_t *) malloc (sizeof(liste_t));
listptr = listptr->next;
listptr->next = 0;
listptr->Zahl = val;
printf("\n");
return lp;
}
else {
listptr = (struct liste_t *) malloc (sizeof(liste_t));
listptr->next = 0;
listptr->Zahl = val;
printf("\n");
return listptr;
}
}