home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
s
/
snip1292.zip
/
LL_MSORT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-11-22
|
2KB
|
71 lines
/*
** Here's an example of how to sort a singly-linked list. I think it
** can be modified to sort a doubly-linked list, but it would get a bit
** more complicated. Note that this is a recursive method, but the
** recursion depth is limited to be proportional to the base 2 log of
** the length of the list, so it won't "run away" and blow the stack.
*/
/* linked list sort -- public domain by Ray Gardner 5/90 */
#include <stdio.h> /* for NULL definition */
typedef struct list_struct {
struct list_struct *next;
char *key;
/* other stuff */
} list;
/* returns < 0 if *p sorts lower than *q */
int keycmp (p, q)
list *p, *q;
{
return strcmp(p->key, q->key);
}
/* merge 2 lists under dummy head item */
list *lmerge (p, q)
list *p, *q;
{
list *r, head;
for ( r = &head; p && q; ) {
if ( keycmp(p, q) < 0 ) {
r = r->next = p;
p = p->next;
} else {
r = r->next = q;
q = q->next;
}
}
r->next = (p ? p : q);
return head.next;
}
/* split list into 2 parts, sort each recursively, merge */
list *lsort (p)
list *p;
{
list *q, *r;
if ( p ) {
q = p;
for ( r = q->next; r && (r = r->next) != NULL; r = r->next )
q = q->next;
r = q->next;
q->next = NULL;
if ( r )
p = lmerge(lsort(r), lsort(p));
}
return p;
}
main ()
{
list *listp; /* pointer to start of list */
/* first build unsorted list, then */
lsort(listp);
}