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
)
278 dumitem
= (listitem
*) malloc (sizeof (listitem
));
279 dumitem
->itemptr
= newitem
;
280 dumitem
->next
= NULL
;
283 if (head
->lastptr
== NULL
)
284 head
->firstitem
= dumitem
;
286 head
->lastptr
->next
= dumitem
;
287 head
->lastptr
= dumitem
;
292 if (head
->firstitem
== NULL
)
294 head
->firstitem
= dumitem
;
299 curitem
= head
->firstitem
;
300 while (curitem
!= NULL
)
303 curitem
= curitem
->next
;
305 previtem
->next
= dumitem
;
314 ** FUNCTIONAL DESCRIPTION:
316 ** unshiftll: add item to the beginning of a linked list
318 ** FORMAL PARAMETERS:
320 ** head: pointer to head of linked list
321 ** newitem: void-pointer to item that has to be inserted in the list
330 int unshiftll (listheader
* head
, void * newitem
)
334 dumitem
= (listitem
*) malloc (sizeof (listitem
));
335 dumitem
->itemptr
= newitem
;
336 dumitem
->next
= head
->firstitem
;
337 head
->firstitem
= dumitem
;
340 if (head
->lastptr
== NULL
)
341 head
->lastptr
= dumitem
;
352 ** FUNCTIONAL DESCRIPTION:
354 ** searchll : find an item in a linked list
356 ** FORMAL PARAMETERS:
358 ** head: pointer to listhead
359 ** item: pointer to item to search for
360 ** compfunction: pointer to a function that can compare to items
361 ** it should return -1, 0, 1. to indicate that the
362 ** first item is smaller, equal, larger than the second
366 ** pointer to the found item, or NULL if not found
370 void * searchll (listheader
* head
, void * item
, int (* compfunction
)())
375 curitem
= head
->firstitem
;
376 while (curitem
!= NULL
)
378 compresult
= (* compfunction
)(curitem
->itemptr
, item
);
381 return (curitem
->itemptr
);
383 else if (compresult
> 0)
387 curitem
= curitem
->next
;
395 ** FUNCTIONAL DESCRIPTION:
397 ** emptyll: remove all items from a linked list
399 ** FORMAL PARAMETERS:
401 ** head: pointer to the listheader
402 ** delfunction: pointer to a function that is called to delete each
403 ** item from the list, it is called with 1 parameter,
404 ** a void-pointer to the item, no return-value is expected
413 int emptyll (listheader
* head
, void (* delfunction
)(void *))
423 curitem
= head
->firstitem
;
424 while (curitem
!= NULL
)
426 (* delfunction
)(curitem
->itemptr
);
427 dumitem
= curitem
->next
;
431 head
->firstitem
= NULL
;
434 head
->lastptr
= NULL
;
445 ** FUNCTIONAL DESCRIPTION:
447 ** doll: execute a function for all the items from a linked list
449 ** FORMAL PARAMETERS:
451 ** head: pointer to the listheader
452 ** dofunction: pointer to a function that is called for each
453 ** item from the list, it is called with 1 parameter,
454 ** a void-pointer to the item, no return-value is expected
463 int doll (listheader
* head
, void (* dofunction
)())
467 curitem
= head
->firstitem
;
468 while (curitem
!= NULL
)
470 (* dofunction
)(curitem
->itemptr
);
471 curitem
= curitem
->next
;
479 ** FUNCTIONAL DESCRIPTION:
481 ** llitembynr: return the data for item nr from the list
483 ** FORMAL PARAMETERS:
485 ** head: pointer to the listheader
486 ** nr : item to return, the first item of the list = 1
495 void * llitembynr (listheader
* head
, int nr
)
506 if (head
->curnr
== (nr
- 1))
507 curitem
= head
->curptr
->next
;
508 else if (head
->curnr
== nr
)
509 curitem
= head
->curptr
;
513 curitem
= head
->firstitem
;
515 while ((curitem
!= NULL
) && (counter
!= nr
))
518 curitem
= curitem
->next
;
528 head
->curptr
= curitem
;
530 return (curitem
->itemptr
);
538 ** FUNCTIONAL DESCRIPTION:
540 ** llrembynr: remove item nr from the list and return pointer to
541 ** the data from this item
543 ** FORMAL PARAMETERS:
545 ** head: pointer to the listheader
546 ** nr : item to remove, the first item of the list = 1
555 void * llrembynr (listheader
* head
, int nr
)
557 listitem
* previtem
= NULL
,
568 // use information from the last llitembynr
569 if (head
->curnr
== (nr
- 1))
571 previtem
= head
->curptr
;
572 curitem
= head
->curptr
->next
;
577 curitem
= head
->firstitem
;
579 while ((curitem
!= NULL
) && (counter
!= nr
))
583 curitem
= curitem
->next
;
591 if (previtem
== NULL
)
593 head
->firstitem
= curitem
->next
;
597 previtem
->next
= curitem
->next
;
600 if (curitem
== head
->lastptr
)
601 head
->lastptr
= previtem
;
606 data
= curitem
->itemptr
;
617 ** FUNCTIONAL DESCRIPTION:
619 ** copyll: copy a complete linked list and all the data in it
621 ** FORMAL PARAMETERS:
623 ** head: pointer to the listheader
624 ** copyfunction: pointer to a function that is called for each
625 ** item from the list, it is called with 1 parameter,
626 ** a void-pointer to the item.
627 ** as return-value, a void pointer to a copy of the input
632 ** pointer to the head of the copied list
636 listheader
* copyll (listheader
* head
, void * (* copyfunction
)())
641 listheader
* newheader
;
648 curitem
= head
->firstitem
;
649 newheader
= (listheader
*) malloc (sizeof (listheader
));
650 newheader
->firstitem
= NULL
;
651 while (curitem
!= NULL
)
653 dumitem
= (listitem
*) malloc (sizeof (listitem
));
655 dumitem
->next
= NULL
;
656 dumitem
->itemptr
= (* copyfunction
)(curitem
->itemptr
);
659 newheader
->firstitem
= dumitem
;
663 newitem
->next
= dumitem
;
667 curitem
= curitem
->next
;
671 newheader
->lastptr
= newitem
;
672 newheader
->curptr
= NULL
;
673 newheader
->curnr
= -1;
682 ** FUNCTIONAL DESCRIPTION:
686 ** FORMAL PARAMETERS:
688 ** head: header of the linked list
689 ** curptr: pointer to the current item in the list
693 ** pointer to the next item in the linked list (the item after curptr)
694 ** the order of the list is the same order then during inclusion
695 ** if curptr is not found in the list, or there is no next item, the
700 void * getnextll (listheader
* head
, void * curptr
)
704 curitem
= head
->firstitem
;
705 while ((curitem
!= NULL
) && (curitem
->itemptr
!= curptr
))
707 curitem
= curitem
->next
;
709 if ((curitem
== NULL
) || (curitem
->next
== NULL
))
714 curitem
= curitem
->next
;
715 return (curitem
->itemptr
);
721 ** FUNCTIONAL DESCRIPTION:
725 ** FORMAL PARAMETERS:
727 ** head: header of the linked list
728 ** checkfunction: function to be called with each item from the list
729 ** and returning 1 or 0 (1: item we are looking for, 0: not)
733 ** number of the last item in the list that makes the checkfunction
738 int findlastll (listheader
* head
, int (* checkfunction
)(void*))
744 curitem
= head
->firstitem
;
745 while (curitem
!= NULL
)
747 if ((* checkfunction
) (curitem
->itemptr
) == 1)
751 curitem
= curitem
->next
;
759 ** returns the number of items in the linked list
761 int lllength (listheader
* head
)
766 curitem
= head
->firstitem
;
767 while (curitem
!= NULL
)
770 curitem
= curitem
->next
;