1 /* evilwm - Minimalist Window Manager for X
2 * Copyright (C) 1999-2009 Ciaran Anscomb <evilwm@6809.org.uk>
3 * see README for license and other details. */
5 /* Basic linked list handling code. Operations that modify the list
6 * return the new list head. */
11 /* Wrap data in a new list container */
12 static struct list
*list_new(void *data
) {
14 new = malloc(sizeof(struct list
));
15 if (!new) return NULL
;
21 /* Add new data to head of list */
22 struct list
*list_prepend(struct list
*list
, void *data
) {
23 struct list
*elem
= list_new(data
);
24 if (!elem
) return list
;
29 /* Add new data to tail of list */
30 struct list
*list_append(struct list
*list
, void *data
) {
31 struct list
*elem
= list_new(data
);
33 if (!elem
) return list
;
34 if (!list
) return elem
;
35 for (iter
= list
; iter
->next
; iter
= iter
->next
);
40 /* Delete list element containing data */
41 struct list
*list_delete(struct list
*list
, void *data
) {
43 if (!data
) return list
;
44 for (elemp
= &list
; *elemp
; elemp
= &(*elemp
)->next
) {
45 if ((*elemp
)->data
== data
) break;
48 struct list
*elem
= *elemp
;
55 /* Move existing list element containing data to head of list */
56 struct list
*list_to_head(struct list
*list
, void *data
) {
57 if (!data
) return list
;
58 list
= list_delete(list
, data
);
59 return list_prepend(list
, data
);
62 /* Move existing list element containing data to tail of list */
63 struct list
*list_to_tail(struct list
*list
, void *data
) {
64 if (!data
) return list
;
65 list
= list_delete(list
, data
);
66 return list_append(list
, data
);
69 /* Find list element containing data */
70 struct list
*list_find(struct list
*list
, void *data
) {
72 for (elem
= list
; elem
; elem
= elem
->next
) {
73 if (elem
->data
== data
)