2 ** FACILITY: linklist.c
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
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)
24 ** Kurt Van den Branden
26 ** CREATION DATE: 22/01/97
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.
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.
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.
67 ** FUNCTIONAL DESCRIPTION:
69 ** newlist: initialize a new linked list
73 ** head: pointer to header of new list
82 int newlist (listheader
* head
)
84 head
->firstitem
= NULL
;
94 ** FUNCTIONAL DESCRIPTION:
96 ** insertll: insert item in a linked list
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
110 ** 1 : item already in the list (0 was returned by compfunction)
114 int insertll (listheader
* head
, void * newitem
, int (* compfunction
)())
126 if (head
->firstitem
== NULL
)
128 dumitem
= (listitem
*) malloc (sizeof (listitem
));
129 dumitem
->itemptr
= newitem
;
130 dumitem
->next
= NULL
;
131 head
->firstitem
= dumitem
;
133 head
->lastptr
= dumitem
;
139 curitem
= head
->firstitem
;
140 while (curitem
!= NULL
)
142 compresult
= (* compfunction
)(curitem
->itemptr
, newitem
);
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
;
158 previtem
->next
= dumitem
;
160 dumitem
->next
= curitem
;
166 curitem
= curitem
->next
;
169 dumitem
= (listitem
*) malloc (sizeof (listitem
));
170 dumitem
->itemptr
= newitem
;
171 dumitem
->next
= NULL
;
172 previtem
->next
= dumitem
;
175 head
->lastptr
= dumitem
;
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
199 ** pointer to data (has to be deleted by calling procedure)
204 void * llremitem (listheader
* head
, void * newitem
, int (* compfunction
)())
211 if ((head
== NULL
) || (head
->firstitem
== NULL
))
217 curitem
= head
->firstitem
;
218 while (curitem
!= NULL
)
220 compresult
= (* compfunction
)(curitem
->itemptr
, newitem
);
223 if (previtem
== NULL
)
225 head
->firstitem
= curitem
->next
;
229 previtem
->next
= curitem
->next
;
232 if (head
->lastptr
== curitem
)
233 head
->lastptr
= previtem
;
237 data
= curitem
->itemptr
;
241 else if (compresult
> 0)
248 curitem
= curitem
->next
;
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
274 int pushll (listheader
* head
, void * newitem
)
280 dumitem
= (listitem
*) malloc (sizeof (listitem
));
281 dumitem
->itemptr
= newitem
;
282 dumitem
->next
= NULL
;
285 if (head
->lastptr
== NULL
)
286 head
->firstitem
= dumitem
;
288 head
->lastptr
->next
= dumitem
;
289 head
->lastptr
= dumitem
;
291 if (head
->firstitem
== NULL
)
293 head
->firstitem
= dumitem
;
298 curitem
= head
->firstitem
;
299 while (curitem
!= NULL
)
302 curitem
= curitem
->next
;
304 previtem
->next
= dumitem
;
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
329 int unshiftll (listheader
* head
, void * newitem
)
333 dumitem
= (listitem
*) malloc (sizeof (listitem
));
334 dumitem
->itemptr
= newitem
;
335 dumitem
->next
= head
->firstitem
;
336 head
->firstitem
= dumitem
;
339 if (head
->lastptr
== NULL
)
340 head
->lastptr
= dumitem
;
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
365 ** pointer to the found item, or NULL if not found
369 void * searchll (listheader
* head
, void * item
, int (* compfunction
)())
374 curitem
= head
->firstitem
;
375 while (curitem
!= NULL
)
377 compresult
= (* compfunction
)(curitem
->itemptr
, item
);
380 return (curitem
->itemptr
);
382 else if (compresult
> 0)
386 curitem
= curitem
->next
;
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
412 int emptyll (listheader
* head
, void (* delfunction
)(void *))
422 curitem
= head
->firstitem
;
423 while (curitem
!= NULL
)
425 (* delfunction
)(curitem
->itemptr
);
426 dumitem
= curitem
->next
;
430 head
->firstitem
= NULL
;
433 head
->lastptr
= NULL
;
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
462 int doll (listheader
* head
, void (* dofunction
)())
466 curitem
= head
->firstitem
;
467 while (curitem
!= NULL
)
469 (* dofunction
)(curitem
->itemptr
);
470 curitem
= curitem
->next
;
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
494 void * llitembynr (listheader
* head
, int nr
)
505 if (head
->curnr
== (nr
- 1))
506 curitem
= head
->curptr
->next
;
507 else if (head
->curnr
== nr
)
508 curitem
= head
->curptr
;
512 curitem
= head
->firstitem
;
514 while ((curitem
!= NULL
) && (counter
!= nr
))
517 curitem
= curitem
->next
;
527 head
->curptr
= curitem
;
529 return (curitem
->itemptr
);
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
554 void * llrembynr (listheader
* head
, int nr
)
556 listitem
* previtem
= NULL
,
567 // use information from the last llitembynr
568 if (head
->curnr
== (nr
- 1))
570 previtem
= head
->curptr
;
571 curitem
= head
->curptr
->next
;
576 curitem
= head
->firstitem
;
578 while ((curitem
!= NULL
) && (counter
!= nr
))
582 curitem
= curitem
->next
;
590 if (previtem
== NULL
)
592 head
->firstitem
= curitem
->next
;
596 previtem
->next
= curitem
->next
;
599 if (curitem
== head
->lastptr
)
600 head
->lastptr
= previtem
;
605 data
= curitem
->itemptr
;
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
631 ** pointer to the head of the copied list
635 listheader
* copyll (listheader
* head
, void * (* copyfunction
)())
640 listheader
* newheader
;
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
);
658 newheader
->firstitem
= dumitem
;
662 newitem
->next
= dumitem
;
666 curitem
= curitem
->next
;
670 newheader
->lastptr
= newitem
;
671 newheader
->curptr
= NULL
;
672 newheader
->curnr
= -1;
681 ** FUNCTIONAL DESCRIPTION:
685 ** FORMAL PARAMETERS:
687 ** head: header of the linked list
688 ** curptr: pointer to the current item in the list
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
699 void * getnextll (listheader
* head
, void * curptr
)
703 curitem
= head
->firstitem
;
704 while ((curitem
!= NULL
) && (curitem
->itemptr
!= curptr
))
706 curitem
= curitem
->next
;
708 if ((curitem
== NULL
) || (curitem
->next
== NULL
))
713 curitem
= curitem
->next
;
714 return (curitem
->itemptr
);
720 ** FUNCTIONAL DESCRIPTION:
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)
732 ** number of the last item in the list that makes the checkfunction
737 int findlastll (listheader
* head
, int (* checkfunction
)(void*))
743 curitem
= head
->firstitem
;
744 while (curitem
!= NULL
)
746 if ((* checkfunction
) (curitem
->itemptr
) == 1)
750 curitem
= curitem
->next
;
758 ** returns the number of items in the linked list
760 int lllength (listheader
* head
)
765 curitem
= head
->firstitem
;
766 while (curitem
!= NULL
)
769 curitem
= curitem
->next
;