1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/* $Header: /home/ksheff/src/e2tools/RCS/elist.c,v 0.6 2004/04/06 19:34:44 ksheff Exp $ */
/*
* elist.c
*
* Copyright (C) 2002 Keith W Sheffield. This file may be redistributed
* under the terms of the GNU Public License.
*
*/
#ifndef ELIST_C
#define ELIST_C
#endif
/* Description */
/*
*
*
*/
/*
* $Log: elist.c,v $
* Revision 0.6 2004/04/06 19:34:44 ksheff
* Corrected elist_delete to update the previous node if it exists.
*
* Revision 0.5 2002/06/05 22:05:01 ksheff
* Added elist_delete.
*
* Revision 0.4 2002/06/03 23:02:01 ksheff
* Added the function elist_sort().
*
* Revision 0.3 2002/04/23 01:49:48 ksheff
* Added a define for NULL if it doesn't exist.
*
* Revision 0.2 2002/03/07 07:33:28 ksheff
* silenced compiler warnings for calloc return value.
*
* Revision 0.1 2002/03/07 07:24:35 ksheff
* initial revision
*
*/
/* Feature Test Switches */
/* Headers */
#include <memory.h>
#include "elist.h"
#ifndef NULL
#define NULL ((void *)0)
#endif
elist_t *
elist_new()
{
elist_t *list;
list = (elist_t *) calloc(sizeof(elist_t), 1);
return list;
}
elist_t *
elist_delete(elist_t *l, void (*data_free)(void *))
{
elist_t *n;
if (l)
{
n = l->next;
if (n)
n->prev = l->prev;
if (l->prev)
l->prev->next = n;
if (data_free && l->data)
(*data_free)(l->data);
free(l);
l = n;
}
return(l);
}
void
elist_free(elist_t *l, void (*data_free)(void *))
{
elist_t *n;
if (l)
{
do
{
l = elist_delete(l, data_free);
} while (l);
}
}
elist_t *
elist_append(elist_t *l, void *data)
{
elist_t *n;
elist_t *t;
if (NULL == (n = elist_new()))
return(NULL);
n->data = data;
if (l)
{
t = l;
while (t->next != NULL) t = t->next;
t->next = n;
n->prev = t;
}
else
l = n;
return(l);
}
elist_t *
elist_insert(elist_t *l, void *data)
{
elist_t *n;
if (NULL == (n = elist_new()))
return(NULL);
n->data = data;
if (l)
{
n->prev = l->prev;
l->prev = n;
n->next = l;
if (n->prev)
n->prev->next = n;
}
l = n;
return(l);
}
void elist_sort(elist_t *l, int (sort_func)(void *, void *), int reverse)
{
int c=0;
elist_t *tl;
void **data;
void **dptr;
if (l != NULL && sort_func != NULL)
{
/* count the number of nodes */
tl = l;
while (tl != NULL)
{
c++;
tl = tl->next;
}
/* if there are more than 1 nodes, allocate a lookup table */
if (c > 1 && (NULL != (data = (void **) malloc(c*sizeof(void*)))))
{
/* fill in the lookup table */
tl = l;
dptr = data;
while (tl != NULL)
{
*dptr++ = tl->data;
tl = tl->next;
}
qsort(data, c, sizeof(dptr), sort_func);
/* now fill in the data pointers in the linked list with the
* data nodes in the correct order
*/
tl = l;
if (reverse)
{
dptr = data + c - 1;
while (tl != NULL)
{
tl->data = *dptr--;
tl = tl->next;
}
}
else
{
dptr = data;
while (tl != NULL)
{
tl->data = *dptr++;
tl = tl->next;
}
}
free(data);
}
}
}
|