wmppp.app uses libdockapp
[dockapps.git] / wmitime / wmgeneral / list.c
blob0b69885ea7085a90fd33bd8b26d1783c12e52450
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)
15 any later version.
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. */
33 #include "list.h"
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37 #include <stdlib.h>
39 /* Return a cons cell produced from (head . tail) */
41 LinkedList*
42 list_cons(void* head, LinkedList* tail)
44 LinkedList* cell;
46 cell = (LinkedList*)malloc(sizeof(LinkedList));
47 cell->head = head;
48 cell->tail = tail;
49 return cell;
52 /* Return the length of a list, list_length(NULL) returns zero */
54 int
55 list_length(LinkedList* list)
57 int i = 0;
58 while(list)
60 i += 1;
61 list = list->tail;
63 return i;
66 /* Return the Nth element of LIST, where N count from zero. If N
67 larger than the list length, NULL is returned */
69 void*
70 list_nth(int index, LinkedList* list)
72 while(index-- != 0)
74 if(list->tail)
75 list = list->tail;
76 else
77 return 0;
79 return list->head;
82 /* Remove the element at the head by replacing it by its successor */
84 void
85 list_remove_head(LinkedList** list)
87 if (!*list) return;
88 if ((*list)->tail)
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 */
96 free(*list);
97 (*list) = 0;
102 /* Remove the element with `car' set to ELEMENT */
104 void
105 list_remove_elem(LinkedList** list, void* elem)
107 while (*list)
109 if ((*list)->head == elem)
110 list_remove_head(list);
111 *list = (*list ? (*list)->tail : NULL);
115 LinkedList *
116 list_remove_elem(LinkedList* list, void* elem)
118 LinkedList *tmp;
120 if (list) {
121 if (list->head == elem) {
122 tmp = list->tail;
123 free(list);
124 return tmp;
126 list->tail = list_remove_elem(list->tail, elem);
127 return list;
129 return NULL;
133 /* Return element that has ELEM as car */
135 LinkedList*
136 list_find(LinkedList* list, void* elem)
138 while(list)
140 if (list->head == elem)
141 return list;
142 list = list->tail;
144 return NULL;
147 /* Free list (backwards recursive) */
149 void
150 list_free(LinkedList* list)
152 if(list)
154 list_free(list->tail);
155 free(list);
159 /* Map FUNCTION over all elements in LIST */
161 void
162 list_mapcar(LinkedList* list, void(*function)(void*))
164 while(list)
166 (*function)(list->head);
167 list = list->tail;