wmCalClock: bump to 1.26
[dockapps.git] / wmcube / wmgeneral / list.c
blob1e3764b05e85472bfec121a755c559f82cded073
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)
16 any later version.
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. */
34 #include "list.h"
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38 #include <stdlib.h>
40 /* Return a cons cell produced from (head . tail) */
42 LinkedList*
43 list_cons(void* head, LinkedList* tail)
45 LinkedList* cell;
47 cell = (LinkedList*)malloc(sizeof(LinkedList));
48 cell->head = head;
49 cell->tail = tail;
50 return cell;
53 /* Return the length of a list, list_length(NULL) returns zero */
55 int
56 list_length(LinkedList* list)
58 int i = 0;
59 while(list)
61 i += 1;
62 list = list->tail;
64 return i;
67 /* Return the Nth element of LIST, where N count from zero. If N
68 larger than the list length, NULL is returned */
70 void*
71 list_nth(int index, LinkedList* list)
73 while(index-- != 0)
75 if(list->tail)
76 list = list->tail;
77 else
78 return 0;
80 return list->head;
83 /* Remove the element at the head by replacing it by its successor */
85 void
86 list_remove_head(LinkedList** list)
88 if (!*list) return;
89 if ((*list)->tail)
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 */
97 free(*list);
98 (*list) = 0;
103 /* Remove the element with `car' set to ELEMENT */
105 void
106 list_remove_elem(LinkedList** list, void* elem)
108 while (*list)
110 if ((*list)->head == elem)
111 list_remove_head(list);
112 *list = (*list ? (*list)->tail : NULL);
116 LinkedList *
117 list_remove_elem(LinkedList* list, void* elem)
119 LinkedList *tmp;
121 if (list) {
122 if (list->head == elem) {
123 tmp = list->tail;
124 free(list);
125 return tmp;
127 list->tail = list_remove_elem(list->tail, elem);
128 return list;
130 return NULL;
134 /* Return element that has ELEM as car */
136 LinkedList*
137 list_find(LinkedList* list, void* elem)
139 while(list)
141 if (list->head == elem)
142 return list;
143 list = list->tail;
145 return NULL;
148 /* Free list (backwards recursive) */
150 void
151 list_free(LinkedList* list)
153 if(list)
155 list_free(list->tail);
156 free(list);
160 /* Map FUNCTION over all elements in LIST */
162 void
163 list_mapcar(LinkedList* list, void(*function)(void*))
165 while(list)
167 (*function)(list->head);
168 list = list->tail;