icl_list.c

Go to the documentation of this file.
00001 
00007 /* $Id: icl_list.c,v 1.4 2004/09/12 05:21:15 seymour Exp $ */
00008 /* $UTK_Copyright: $ */
00009 
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 
00013 #include "icl_list.h"
00014 
00021 icl_list_t *
00022 icl_list_new()
00023 {
00024   icl_list_t *node;
00025 
00026   node = (icl_list_t*)malloc(sizeof(icl_list_t));
00027 
00028   if(!node) return NULL;
00029 
00030   node->flink = NULL;
00031   node->blink = node;
00032   node->data = NULL;
00033 
00034   return(node);
00035 }
00036 
00048 icl_list_t *
00049 icl_list_insert(icl_list_t *head, icl_list_t *pos, void *data)
00050 {
00051   icl_list_t *node;
00052 
00053   if(!head || !pos) return NULL;
00054 
00055   node = (icl_list_t*)malloc(sizeof(icl_list_t));
00056 
00057   if(!node) return NULL;
00058 
00059   node->blink = pos;
00060   node->flink = pos->flink;
00061   node->data = data;
00062 
00063   if(pos->flink) 
00064     pos->flink->blink = node;
00065   else 
00066     head->blink = node; /* node at end of list */
00067 
00068   pos->flink = node;
00069 
00070   return node;
00071 }
00072 
00083 int 
00084 icl_list_delete(icl_list_t *head, icl_list_t *pos, void (*free_function)(void *)) 
00085 {
00086   if (!pos || !head) return -1;
00087   if (pos == head) return -1;
00088 
00089   if(free_function && pos->data) 
00090     (*free_function)(pos->data);
00091 
00092   pos->blink->flink = pos->flink;
00093 
00094   if(pos->flink) 
00095     pos->flink->blink = pos->blink; 
00096   else 
00097     head->blink = pos->blink;   /* pos at end of list */
00098 
00099   free(pos);
00100 
00101   return 0;
00102 }
00103 
00114 icl_list_t *
00115 icl_list_search(icl_list_t *head, void *data, int (*compare)(void*, void*))
00116 {
00117   icl_list_t *node;
00118 
00119   if (!head) return NULL;
00120 
00121   for (node=head->flink; node!=NULL; node=node->flink) {
00122     if(!node->data) 
00123       continue;
00124     if((compare && (*compare)(node->data, data)==0)) 
00125       break;
00126     else if (node->data==data) 
00127       break; /* compare == NULL, then direct comparison of pointers */
00128   }
00129 
00130   return(node);
00131 }
00132 
00142 int
00143 icl_list_destroy(icl_list_t *head, void (*free_function)(void*))
00144 {
00145   icl_list_t *node, *tmpnode;
00146 
00147   if (!head) return -1;
00148 
00149   for(node=head; node!=NULL; ) {
00150     tmpnode = node->flink;
00151 
00152     if(free_function!=NULL && node->data!=NULL) 
00153       (*free_function)(node->data);
00154 
00155     free(node);
00156     node = tmpnode;
00157   }
00158 
00159   return 0;
00160 }
00161 
00170 int
00171 icl_list_size(icl_list_t *head)
00172 {
00173   int size=0;
00174 
00175   if(!head) return -1;
00176 
00177   while((head=head->flink)) 
00178     size++;
00179 
00180   return size;
00181 }
00182 
00191 icl_list_t *
00192 icl_list_first(icl_list_t *head)
00193 {
00194   if(head) 
00195     return head->flink;
00196 
00197   return NULL;
00198 }
00199 
00209 icl_list_t *
00210 icl_list_next(icl_list_t *head, icl_list_t *pos)
00211 {
00212   if(pos) 
00213     return pos->flink;
00214 
00215   return NULL;
00216 }
00217 
00227 icl_list_t *
00228 icl_list_prev(icl_list_t *head, icl_list_t *pos) 
00229 {
00230   if(pos && pos->blink!=NULL) 
00231     return pos->blink;
00232 
00233   return NULL;
00234 }
00235 
00246 icl_list_t *
00247 icl_list_concat(icl_list_t *head1, icl_list_t *head2)
00248 {
00249   if(!head1 || !head2 || !head1->blink || !head2->flink) 
00250     return NULL;
00251 
00252   head1->blink->flink = head2->flink;
00253   head2->flink->blink = head1->blink;
00254   head1->blink = head2->blink;
00255 
00256   free(head2);
00257 
00258   return(head1);
00259 }
00260 
00270 icl_list_t *
00271 icl_list_prepend(icl_list_t *head, void *data)
00272 {
00273   return(icl_list_insert(head, head, data));
00274 }
00275 
00285 icl_list_t *
00286 icl_list_append(icl_list_t *head, void *data)
00287 {
00288   return(icl_list_insert(head, head->blink, data));
00289 }