is there a good guide someone can point me to?
I'm trying to learn from my lecture notes, but they aren't very descriptive.
my books don't mention them neither.
Printable View
is there a good guide someone can point me to?
I'm trying to learn from my lecture notes, but they aren't very descriptive.
my books don't mention them neither.
You should be able to google "C linked list tutorial" and find a ton. Sorting may or may not be included; sometimes sorting is discussed in terms of an algorithm in pseudo code rather than a particular language (there are about half a dozen common algorithms).
Sorting is easiest with integers, so give your nodes some kind of number to work with
You can try sorting strings once you get numbers to work. Of the sorts I've implimented I would say the order of difficulty is something likeCode:typedef struct _node {
char name[32];
int number;
struct _node *next;
} node;
- bubblesort (easiest)
- insertion sort
- selection sort
- merge sort
- quick sort (trickiest)
Write each sort as a single function that calls a separate simple comparison routine (then you can swap a different routine in later for dealing with strings). The comparison for ints can be this simple:
which could be a macro -- but some of the sort algorithms are recursive and using a macro there can cause a problem. The recursive sorts (merge and quick) require two separate functions.Code:int compare_numbers (int a, int b) {
if (a==b) return 0;
if (a>b) return 2;
else return 1;
}
how about
Linked list - Wikipedia, the free encyclopedia
while inserting an element into the link list, check where it would belong according to the element's magnitude,compared to the rest of the elements already present in the LL
then you got yourself a sorted linked list.