| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | LISTLINK.C 1/5 |
JG> I have a bunch of strings in a doubly linked list, that I
JG> need to sort into alphabetical order. can some one give
JG> me an out line or even some code on how I should go about
JG> doing this?
How about a really simple example of linked lists with a sort
routine included?
/*_|_| LISTLINK.C ( part 1 of 5 )
_|_|_| Heavily commented example of using linked lists in C.
_|_|_| No warrantee or guarantee is given or implied.
_|_|_| PUBLIC DOMAIN by Kurt Kuzba. (7/3/1998)*/
#include
#include
#include
#include
typedef struct {
void *item, *next, *prev; /* This structure provides the */
} LinkedList; /* basic framework for the list. */
typedef struct {
int items, iterator; /* This structure provides the */
LinkedList *it; /* space to store list data. */
} ListData;
LinkedList *Init_List(void)
{ /* This will initialize a list. */
LinkedList *LL = malloc(sizeof(LinkedList));
ListData *LD; /* First, reserve memory for the */
if(LL) /* list itself, and then for the */
{ /* list data, stored as item 0. */
LL->next = LL->prev = NULL;
LL->item = malloc(sizeof(ListData));
if(LL->item) /* We need to initialize first */
{ /* and last link to NULL, and */
LD = LL->item; /* set our iterator to 0 also. */
LD->items = LD->iterator = 0;
LD->it = LL;
} /* The pointer we return will be */
} /* used from now on to identify */
return LL; /* our list in function calls. */
}
int Count_List(LinkedList *LL)
{
ListData *LD = LL->item; /* Since we keep track of how */
return LD->items; /* many items we have, this is */
} /* a very simple function. */
LinkedList *List_Walk(LinkedList *LL, int i)
{ /* This will return a pointer */
int walk; /* to a specific list member. */
ListData *LD = LL->item; /* Set up a ListData pointer. */
if(i LD->items) /* Test for viability of i. */
return NULL;
if(i items / 2)) /* Decide if forward walk will */
{ /* be faster than backward walk. */
for(walk = 0; walk < i; walk++)
LL = LL->next; /* take next pointer til i. */
}
else
{
for(walk = LD->items + 1; walk > i; walk--)
LL = LL->prev; /* take prev pointer til i. */
}
return LL; /* return pointer to list member.*/
}
void *Walk_List(LinkedList *LL, int i)
{ /* This returns an item pointer. */
return List_Walk(LL, i)->item;
}
void Inc_It(LinkedList *LL)
{ /* Icrement the list iterator. */
ListData *LD = LL->item; /* Set up a ListData pointer. */
if(LD->items > LD->iterator)/* Test for last list element. */
{
LD->it = LD->it->next; /* Set iterator to next element. */
LD->iterator++; /* Update iterator numerator. */
}
else
{
if(LD->items)
{
LD->it = LL->next; /* Wrap to first list element. */
LD->iterator = 1; /* Update iterator numerator. */
}
}
}
/*_|_| end LISTLINK.C ( part 1 of 5 ) */
> ] * Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728 SEEN-BY: 639/252 670/213 218 @PATH: 154/750 222 396/1 633/260 635/506 728 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.