ok some one might complain but this is C code on top of that, so you will have to convert it if you figure out how to use it, it sorts by alphabetical order using the names in the nodes. just need to convert it to use int values . should work? Just trying to help , it is what I used after I had to convert it to do what I wanted it to do , that in which it now does.
C file
Code:
// in your comparison here
// val < or > then the other
int cmp_filename(mylist *file1, mylist *file2)
{
return(strcmp(file1->filename, file2->filename));
}
mylist *list_sort(mylist * list, compare_fn cmp)
{
mylist *l1, *l2;
if (!list)
return (NULL);
if (!list->next)
return (list);
l1 = list;
l2 = list->next;
while ((l2 = l2->next) != NULL)
{
if ((l2 = l2->next) == NULL)
break;
l1 = l1->next;
}
l2 = l1->next;
l1->next = NULL;
return (list_sort_merge
(list_sort(list, cmp), list_sort(l2, cmp), cmp));
}
mylist *list_sort_merge(mylist * l1, mylist * l2 , compare_fn cmp)
{
mylist list, *l, *lprev;
l = &list;
lprev = NULL;
while (l1 && l2)
{
if (strcmp(l1->filename, l2->filename) < 0)
{
l->next = l1;
l = l->next;
l->prev = lprev;
lprev = l;
l1 = l1->next;
}
else
{
l->next = l2;
l = l->next;
l->prev = lprev;
lprev = l;
l2 = l2->next;
}
}
l->next = l1 ? l1 : l2;
l->next->prev = l;
return (list.next);
}
in the header file:
defines for the functions.
Code:
typedef int (compare_fn) (mylist *name1, mylist *name2);
Code:
// sorted List function
// how to call it.
/*
filelist = list_sort(filelist, cmp_filename);
*/
I do not know if you can use theses but you should be able to just change the data types and how it compares the values one greater than the other, gives
you the results to tell the other function what to do with it.
I think that is everything. so yes a their still is a little work left to be needed to do which requires thinking
PS if this just happened to be for a school project well ya better understand how it really works if you figure out how to get it to work.
sort and print are two separate functions btw.
this works after everything is gotten which is faster then my code I used while adding to the list, if you are sorting before adding to the list using number values then you have to add that code to you add node function t, which is just a little easier. then this code is. I'd think a lot less code as well. using an algorithm of one type or other.