Support for g++-4.1.
[gf1.git] / linklist.c
blob9b2a2159df88bbee5b26d388bc63a7c35c6b2976
1 /*
2 ** FACILITY: linklist.c
3 **
4 ** MODULE DESCRIPTION:
5 **
6 ** general routines for linked lists
7 ** + newlist: initialize new list
8 ** + insertll: insert item in list
9 ** + searchll: search for item in list
10 ** + emptyll: delete all items from a linked list
11 ** + doll: run a function for every item from the linked list
12 ** + getnextll: get the next item from the list
13 ** + copyll: copy a complete linked list
14 ** + llitembynr: return pointer to the data of item nr of the list
15 ** + llrembynr: remove item nr from the list and return pointer
16 ** to the data
17 ** + pushll: add item to the end of a linked list
18 ** + findlastll: find last item in the last that
19 ** corresponds with what we want
20 ** (+ delll: delete an item from the list)
22 ** AUTHORS:
24 ** Kurt Van den Branden
26 ** CREATION DATE: 22/01/97
28 ** DESIGN ISSUES:
30 ** these functions should be as general as possible
31 ** you can put any data at the nodes from the list
32 ** you have to provide your own compare-routine
36 ** Copyright (C) 1998 Kurt Van den Branden
38 ** This program is free software; you can redistribute it and/or modify
39 ** it under the terms of the GNU General Public License as published by
40 ** the Free Software Foundation; either version 2 of the License, or
41 ** (at your option) any later version.
42 **
43 ** This program is distributed in the hope that it will be useful,
44 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
45 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 ** GNU General Public License for more details.
47 **
48 ** You should have received a copy of the GNU General Public License
49 ** along with this program; if not, write to the Free Software
50 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
56 ** INCLUDE FILES
60 #include <stdio.h>
61 #include <malloc.h>
62 #include "linklist.h"
64 #ifdef FASTLLIST
66 **++
67 ** FUNCTIONAL DESCRIPTION:
69 ** newlist: initialize a new linked list
71 ** FORMAL PARAMETERS:
73 ** head: pointer to header of new list
75 ** RETURN VALUE:
77 ** 0: OK
78 ** -1: error
80 **--
82 int newlist (listheader * head)
84 head->firstitem = NULL;
85 head->curnr = -1;
86 head->curptr = NULL;
87 head->lastptr = NULL;
88 return (0);
90 #endif
93 **++
94 ** FUNCTIONAL DESCRIPTION:
96 ** insertll: insert item in a linked list
98 ** FORMAL PARAMETERS:
100 ** head: pointer to head of linked list
101 ** newitem: void-pointer to item that has to be inserted in the list
102 ** compfunction: pointer to a function that can compare to items
103 ** it should return -1, 0, 1. to indicate that the
104 ** first item is smaller, equal, larger than the second
106 ** RETURN VALUE:
108 ** 0 : OK
109 ** -1: error
110 ** 1 : item already in the list (0 was returned by compfunction)
112 **--
114 int insertll (listheader * head, void * newitem, int (* compfunction)())
116 listitem *curitem,
117 *previtem,
118 *dumitem;
119 int compresult;
121 #ifdef FASTLLIST
122 head->curnr = -1;
123 head->curptr = NULL;
124 #endif
126 if (head->firstitem == NULL)
128 dumitem = (listitem *) malloc (sizeof (listitem));
129 dumitem->itemptr = newitem;
130 dumitem->next = NULL;
131 head->firstitem = dumitem;
132 #ifdef FASTLLIST
133 head->lastptr = dumitem;
134 #endif
135 return (0);
138 previtem = NULL;
139 curitem = head->firstitem;
140 while (curitem != NULL)
142 compresult = (* compfunction)(curitem->itemptr, newitem);
143 if (compresult == 0)
145 return (1);
147 else if (compresult > 0)
149 dumitem = (listitem *) malloc (sizeof (listitem));
150 dumitem->itemptr = newitem;
151 dumitem->next = NULL;
152 if (previtem == NULL)
154 head->firstitem = dumitem;
156 else
158 previtem->next = dumitem;
160 dumitem->next = curitem;
161 return (0);
163 else
165 previtem = curitem;
166 curitem = curitem->next;
169 dumitem = (listitem *) malloc (sizeof (listitem));
170 dumitem->itemptr = newitem;
171 dumitem->next = NULL;
172 previtem->next = dumitem;
174 #ifdef FASTLLIST
175 head->lastptr = dumitem;
176 #endif
178 return (0);
183 **++
184 ** FUNCTIONAL DESCRIPTION:
186 ** llremitem: delete item from a linked list
188 ** FORMAL PARAMETERS:
190 ** head: pointer to head of linked list
191 ** newitem: void-pointer to an item that has to be compared with
192 ** what has to be deleted
193 ** compfunction: pointer to a function that can compare to items
194 ** it should return -1, 0, 1. to indicate that the
195 ** first item is smaller, equal, larger than the second
197 ** RETURN VALUE:
199 ** pointer to data (has to be deleted by calling procedure)
200 ** NULL
202 **--
204 void * llremitem (listheader * head, void * newitem, int (* compfunction)())
206 listitem *curitem,
207 *previtem;
208 int compresult;
209 void * data;
211 if ((head == NULL) || (head->firstitem == NULL))
213 return (NULL);
216 previtem = NULL;
217 curitem = head->firstitem;
218 while (curitem != NULL)
220 compresult = (* compfunction)(curitem->itemptr, newitem);
221 if (compresult == 0)
223 if (previtem == NULL)
225 head->firstitem = curitem->next;
227 else
229 previtem->next = curitem->next;
231 #ifdef FASTLLIST
232 if (head->lastptr == curitem)
233 head->lastptr = previtem;
234 head->curptr = NULL;
235 head->curnr = -1;
236 #endif
237 data = curitem->itemptr;
238 free (curitem);
239 return (data);
241 else if (compresult > 0)
243 return (NULL);
245 else
247 previtem = curitem;
248 curitem = curitem->next;
252 return (NULL);
257 **++
258 ** FUNCTIONAL DESCRIPTION:
260 ** pushll: add item to the end of a linked list
262 ** FORMAL PARAMETERS:
264 ** head: pointer to head of linked list
265 ** newitem: void-pointer to item that has to be inserted in the list
267 ** RETURN VALUE:
269 ** 0 : OK
270 ** -1: error
272 **--
274 int pushll (listheader * head, void * newitem)
276 listitem *curitem,
277 *previtem,
278 *dumitem;
280 dumitem = (listitem *) malloc (sizeof (listitem));
281 dumitem->itemptr = newitem;
282 dumitem->next = NULL;
284 #ifdef FASTLLIST
285 if (head->lastptr == NULL)
286 head->firstitem = dumitem;
287 else
288 head->lastptr->next = dumitem;
289 head->lastptr = dumitem;
290 #else
291 if (head->firstitem == NULL)
293 head->firstitem = dumitem;
294 return (0);
297 previtem = NULL;
298 curitem = head->firstitem;
299 while (curitem != NULL)
301 previtem = curitem;
302 curitem = curitem->next;
304 previtem->next = dumitem;
305 #endif
307 return (0);
312 **++
313 ** FUNCTIONAL DESCRIPTION:
315 ** unshiftll: add item to the beginning of a linked list
317 ** FORMAL PARAMETERS:
319 ** head: pointer to head of linked list
320 ** newitem: void-pointer to item that has to be inserted in the list
322 ** RETURN VALUE:
324 ** 0 : OK
325 ** -1: error
327 **--
329 int unshiftll (listheader * head, void * newitem)
331 listitem *dumitem;
333 dumitem = (listitem *) malloc (sizeof (listitem));
334 dumitem->itemptr = newitem;
335 dumitem->next = head->firstitem;
336 head->firstitem = dumitem;
338 #ifdef FASTLLIST
339 if (head->lastptr == NULL)
340 head->lastptr = dumitem;
341 head->curptr = NULL;
342 head->curnr = -1;
343 #endif
345 return (0);
350 **++
351 ** FUNCTIONAL DESCRIPTION:
353 ** searchll : find an item in a linked list
355 ** FORMAL PARAMETERS:
357 ** head: pointer to listhead
358 ** item: pointer to item to search for
359 ** compfunction: pointer to a function that can compare to items
360 ** it should return -1, 0, 1. to indicate that the
361 ** first item is smaller, equal, larger than the second
363 ** RETURN VALUE:
365 ** pointer to the found item, or NULL if not found
367 **--
369 void * searchll (listheader * head, void * item, int (* compfunction)())
371 listitem * curitem;
372 int compresult;
374 curitem = head->firstitem;
375 while (curitem != NULL)
377 compresult = (* compfunction)(curitem->itemptr, item);
378 if (compresult == 0)
380 return (curitem->itemptr);
382 else if (compresult > 0)
384 return (NULL);
386 curitem = curitem->next;
388 return (NULL);
393 **++
394 ** FUNCTIONAL DESCRIPTION:
396 ** emptyll: remove all items from a linked list
398 ** FORMAL PARAMETERS:
400 ** head: pointer to the listheader
401 ** delfunction: pointer to a function that is called to delete each
402 ** item from the list, it is called with 1 parameter,
403 ** a void-pointer to the item, no return-value is expected
405 ** RETURN VALUE:
407 ** 0: OK
408 ** -1: error
410 **--
412 int emptyll (listheader * head, void (* delfunction)(void *))
414 listitem * curitem,
415 * dumitem;
417 if (head == NULL)
419 return (0);
422 curitem = head->firstitem;
423 while (curitem != NULL)
425 (* delfunction)(curitem->itemptr);
426 dumitem = curitem->next;
427 free (curitem);
428 curitem = dumitem;
430 head->firstitem = NULL;
432 #ifdef FASTLLIST
433 head->lastptr = NULL;
434 head->curptr = NULL;
435 head->curnr = -1;
436 #endif
438 return (0);
443 **++
444 ** FUNCTIONAL DESCRIPTION:
446 ** doll: execute a function for all the items from a linked list
448 ** FORMAL PARAMETERS:
450 ** head: pointer to the listheader
451 ** dofunction: pointer to a function that is called for each
452 ** item from the list, it is called with 1 parameter,
453 ** a void-pointer to the item, no return-value is expected
455 ** RETURN VALUE:
457 ** 0: OK
458 ** -1: error
460 **--
462 int doll (listheader * head, void (* dofunction)())
464 listitem * curitem;
466 curitem = head->firstitem;
467 while (curitem != NULL)
469 (* dofunction)(curitem->itemptr);
470 curitem = curitem->next;
472 return (0);
477 **++
478 ** FUNCTIONAL DESCRIPTION:
480 ** llitembynr: return the data for item nr from the list
482 ** FORMAL PARAMETERS:
484 ** head: pointer to the listheader
485 ** nr : item to return, the first item of the list = 1
487 ** RETURN VALUE:
489 ** pointer to data
490 ** NULL on error
492 **--
494 void * llitembynr (listheader * head, int nr)
496 listitem * curitem;
497 int counter;
499 if (head == NULL)
501 return (NULL);
504 #ifdef FASTLLIST
505 if (head->curnr == (nr - 1))
506 curitem = head->curptr->next;
507 else if (head->curnr == nr)
508 curitem = head->curptr;
509 else
511 #endif
512 curitem = head->firstitem;
513 counter = 1;
514 while ((curitem != NULL) && (counter != nr))
516 counter++;
517 curitem = curitem->next;
519 #ifdef FASTLLIST
521 #endif
523 if (curitem != NULL)
525 #ifdef FASTLLIST
526 head->curnr = nr;
527 head->curptr = curitem;
528 #endif
529 return (curitem->itemptr);
531 return (NULL);
536 **++
537 ** FUNCTIONAL DESCRIPTION:
539 ** llrembynr: remove item nr from the list and return pointer to
540 ** the data from this item
542 ** FORMAL PARAMETERS:
544 ** head: pointer to the listheader
545 ** nr : item to remove, the first item of the list = 1
547 ** RETURN VALUE:
549 ** pointer to data
550 ** NULL on error
552 **--
554 void * llrembynr (listheader * head, int nr)
556 listitem * previtem = NULL,
557 * curitem;
558 int counter;
559 void * data;
561 if (head == NULL)
563 return (NULL);
566 #ifdef FASTLLIST
567 // use information from the last llitembynr
568 if (head->curnr == (nr - 1))
570 previtem = head->curptr;
571 curitem = head->curptr->next;
573 else
575 #endif
576 curitem = head->firstitem;
577 counter = 1;
578 while ((curitem != NULL) && (counter != nr))
580 counter++;
581 previtem = curitem;
582 curitem = curitem->next;
584 #ifdef FASTLLIST
586 #endif
588 if (curitem != NULL)
590 if (previtem == NULL)
592 head->firstitem = curitem->next;
594 else
596 previtem->next = curitem->next;
598 #ifdef FASTLLIST
599 if (curitem == head->lastptr)
600 head->lastptr = previtem;
601 head->curptr = NULL;
602 head->curnr = -1;
603 #endif
605 data = curitem->itemptr;
606 free (curitem);
607 return (data);
610 return (NULL);
615 **++
616 ** FUNCTIONAL DESCRIPTION:
618 ** copyll: copy a complete linked list and all the data in it
620 ** FORMAL PARAMETERS:
622 ** head: pointer to the listheader
623 ** copyfunction: pointer to a function that is called for each
624 ** item from the list, it is called with 1 parameter,
625 ** a void-pointer to the item.
626 ** as return-value, a void pointer to a copy of the input
627 ** item is expected
629 ** RETURN VALUE:
631 ** pointer to the head of the copied list
633 **--
635 listheader * copyll (listheader * head, void * (* copyfunction)())
637 listitem * curitem,
638 * newitem = NULL,
639 * dumitem;
640 listheader * newheader;
642 if (head == NULL)
644 return (NULL);
647 curitem = head->firstitem;
648 newheader = (listheader *) malloc (sizeof (listheader));
649 newheader->firstitem = NULL;
650 while (curitem != NULL)
652 dumitem = (listitem *) malloc (sizeof (listitem));
654 dumitem->next = NULL;
655 dumitem->itemptr = (* copyfunction)(curitem->itemptr);
656 if (newitem == NULL)
658 newheader->firstitem = dumitem;
660 else
662 newitem->next = dumitem;
664 newitem = dumitem;
666 curitem = curitem->next;
669 #ifdef FASTLLIST
670 newheader->lastptr = newitem;
671 newheader->curptr = NULL;
672 newheader->curnr = -1;
673 #endif
675 return (newheader);
680 **++
681 ** FUNCTIONAL DESCRIPTION:
683 ** getnextll
685 ** FORMAL PARAMETERS:
687 ** head: header of the linked list
688 ** curptr: pointer to the current item in the list
690 ** RETURN VALUE:
692 ** pointer to the next item in the linked list (the item after curptr)
693 ** the order of the list is the same order then during inclusion
694 ** if curptr is not found in the list, or there is no next item, the
695 ** NULL is returned.
697 **--
699 void * getnextll (listheader * head, void * curptr)
701 listitem * curitem;
703 curitem = head->firstitem;
704 while ((curitem != NULL) && (curitem->itemptr != curptr))
706 curitem = curitem->next;
708 if ((curitem == NULL) || (curitem->next == NULL))
710 return (NULL);
713 curitem = curitem->next;
714 return (curitem->itemptr);
719 **++
720 ** FUNCTIONAL DESCRIPTION:
722 ** findlastll
724 ** FORMAL PARAMETERS:
726 ** head: header of the linked list
727 ** checkfunction: function to be called with each item from the list
728 ** and returning 1 or 0 (1: item we are looking for, 0: not)
730 ** RETURN VALUE:
732 ** number of the last item in the list that makes the checkfunction
733 ** return 1
735 **--
737 int findlastll (listheader * head, int (* checkfunction)(void*))
739 listitem * curitem;
740 int theitem = 0,
741 nr = 1;
743 curitem = head->firstitem;
744 while (curitem != NULL)
746 if ((* checkfunction) (curitem->itemptr) == 1)
747 theitem = nr;
749 nr++;
750 curitem = curitem->next;
753 return (theitem);
758 ** returns the number of items in the linked list
760 int lllength (listheader * head)
762 listitem * curitem;
763 int counter = 0;
765 curitem = head->firstitem;
766 while (curitem != NULL)
768 counter++;
769 curitem = curitem->next;
772 return (counter);