# learning how to do sorted linked lists

• 03-29-2009
ktran03
learning how to do sorted linked lists
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.
• 03-29-2009
MK27
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
Code:

```typedef struct _node {         char name[32];         int number;         struct _node *next; } node;```
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 like
• 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:
Code:

```int compare_numbers (int a, int b) {         if (a==b) return 0;         if (a>b) return 2;         else return 1; }```
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.
• 03-29-2009
creeping death