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
9 Modified by Douglas Torrance
11 This file is part of GNU CC.
13 GNU CC is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
18 GNU CC is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GNU CC; see the file COPYING. If not, write to
25 the Free Software Foundation, 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA. */
28 /* As a special exception, if you link this library with files compiled with
29 GCC to produce an executable, this does not cause the resulting executable
30 to be covered by the GNU General Public License. This exception does not
31 however invalidate any other reasons why the executable file might be
32 covered by the GNU General Public License. */
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
40 /* Return a cons cell produced from (head . tail) */
43 list_cons(void* head
, LinkedList
* tail
)
47 cell
= (LinkedList
*)malloc(sizeof(LinkedList
));
53 /* Return the length of a list, list_length(NULL) returns zero */
56 list_length(LinkedList
* list
)
67 /* Return the Nth element of LIST, where N count from zero. If N
68 larger than the list length, NULL is returned */
71 list_nth(int index
, LinkedList
* list
)
83 /* Remove the element at the head by replacing it by its successor */
86 list_remove_head(LinkedList
** list
)
91 LinkedList
* tail
= (*list
)->tail
; /* fetch next */
92 *(*list
) = *tail
; /* copy next to list head */
93 free(tail
); /* free next */
95 else /* only one element in list */
103 /* Remove the element with `car' set to ELEMENT */
106 list_remove_elem(LinkedList** list, void* elem)
110 if ((*list)->head == elem)
111 list_remove_head(list);
112 *list = (*list ? (*list)->tail : NULL);
117 list_remove_elem(LinkedList
* list
, void* elem
)
122 if (list
->head
== elem
) {
127 list
->tail
= list_remove_elem(list
->tail
, elem
);
134 /* Return element that has ELEM as car */
137 list_find(LinkedList
* list
, void* elem
)
141 if (list
->head
== elem
)
148 /* Free list (backwards recursive) */
151 list_free(LinkedList
* list
)
155 list_free(list
->tail
);
160 /* Map FUNCTION over all elements in LIST */
163 list_mapcar(LinkedList
* list
, void(*function
)(void*))
167 (*function
)(list
->head
);