WARNING Most likely, it will be redundant for what you want to do. But I'm upset, trying to track a subtle, but unpleasant mistake and need to be distracted.
If I can offer some suggestions ...
First of all, IMO is easier to insert items into a list in order than to sort an unordered list; what I would do is create an insertInOrder function that takes away your list title, a new item to add, and a predicate (function pointer) that compares entries and returns a new chapter to the list:
Rec *insertInOrder(Rec *head, Rec *new, int (*cmp)(Rec *, Rec *)) { if (head == NULL) { return new; } else if (cmp(new, head) < 0)
Now you can order the list based on various criteria. The cmp argument points to a function that takes two record pointers and compares them, returning -1 if the first argument is less than the second, 1 if the first argument is greater than the second, and 0 if they compare equal. For example, if you want to sort by name, define a function like
int compareNames(Rec *e1, Rec *e2) { int r = strcmp(e1->name, e2->name); if (r < 0) return -1; if (r > 0) return 1; return 0; }
and call insertInOrder as
head = insertInOrder(head, new, compareNames);
To sort the list specified by a specific predicate: starting from the head, delete one item at a time from the existing list and add it to the new list using the specified predicate:
Rec *sortLinkedList(Rec *head, int (*cmp)(Rec *, Rec *)) { Rec *newList = NULL; while (head) { Rec *tmp = head; head = head->nextRec; tmp->nextRec = NULL; newList = insertInOrder(newList, tmp, cmp); } return newList; } ... head = sortLinkedList(head, compareNr); ... head = sortLinkdeList(head, compareNames); ... head = sortLinkedList(head, compareSomethingElse);
Like Neil, I don't really like typedefs for pointer types; experience has shown me that they cause more problems than they are worth it.