1 /* Generic single linked list to keep various information
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 Author: Kresten Krab Thorup
7 Many modifications by Alfredo K. Kojima
10 This file is part of GNU CC.
12 GNU CC is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 GNU CC is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GNU CC; see the file COPYING. If not, write to
24 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301 USA. */
27 /* As a special exception, if you link this library with files compiled with
28 GCC to produce an executable, this does not cause the resulting executable
29 to be covered by the GNU General Public License. This exception does not
30 however invalidate any other reasons why the executable file might be
31 covered by the GNU General Public License. */
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
39 /* Return a cons cell produced from (head . tail) */
42 list_cons(void* head
, LinkedList
* tail
)
46 cell
= (LinkedList
*)malloc(sizeof(LinkedList
));
52 /* Return the length of a list, list_length(NULL) returns zero */
55 list_length(LinkedList
* list
)
66 /* Return the Nth element of LIST, where N count from zero. If N
67 larger than the list length, NULL is returned */
70 list_nth(int index
, LinkedList
* list
)
82 /* Remove the element at the head by replacing it by its successor */
85 list_remove_head(LinkedList
** list
)
90 LinkedList
* tail
= (*list
)->tail
; /* fetch next */
91 *(*list
) = *tail
; /* copy next to list head */
92 free(tail
); /* free next */
94 else /* only one element in list */
102 /* Remove the element with `car' set to ELEMENT */
105 list_remove_elem(LinkedList** list, void* elem)
109 if ((*list)->head == elem)
110 list_remove_head(list);
111 *list = (*list ? (*list)->tail : NULL);
116 list_remove_elem(LinkedList
* list
, void* elem
)
121 if (list
->head
== elem
) {
126 list
->tail
= list_remove_elem(list
->tail
, elem
);
133 /* Return element that has ELEM as car */
136 list_find(LinkedList
* list
, void* elem
)
140 if (list
->head
== elem
)
147 /* Free list (backwards recursive) */
150 list_free(LinkedList
* list
)
154 list_free(list
->tail
);
159 /* Map FUNCTION over all elements in LIST */
162 list_mapcar(LinkedList
* list
, void(*function
)(void*))
166 (*function
)(list
->head
);