2 * ll.c - various things of used for dealing with linked lists.
4 * Copyright 2000-2004 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 struct ll *lladd(struct ll *curll, void *object)
30 if ((new = malloc(sizeof(struct ll))) == NULL) {
32 printf("Got NULL in lladd()\n");
42 struct ll *lladdend(struct ll *curll, void *object)
47 if ((new = malloc(sizeof(struct ll))) == NULL) {
48 logthing(LOGTHING_ERROR,
49 "Couldn't allocate memory in lladdend()");
58 while (cur->next != NULL) {
69 struct ll *lldel(struct ll *curll, void *object,
70 int (*objectcmp) (const void *object1, const void *object2))
72 struct ll *cur = NULL;
73 struct ll *old = NULL;
75 log_assert(objectcmp != NULL);
80 } else if (!(*objectcmp)(cur->object, object)) {
86 while (cur->next != NULL) {
87 if (!(*objectcmp)(cur->next->object, object)) {
89 cur->next = cur->next->next;
97 struct ll *llfind(struct ll *curll, void *object,
98 int (*objectcmp) (const void *object1, const void *object2))
102 log_assert(objectcmp != NULL);
105 while (cur != NULL && (*objectcmp)(cur->object, object)) {
111 unsigned long llsize(struct ll *curll)
113 unsigned long count = 0;
115 while (curll != NULL) {
124 * llfree - Frees a linked list.
125 * @curll: The list to free.
126 * @objectfree: A pointer to a free function for the object.
128 * Walks through a list and free it. If a function is provided for
129 * objectfree then it's called for each element to free them, if it's NULL
130 * just the list is freed.
132 void llfree(struct ll *curll, void (*objectfree) (void *object))
136 while (curll != NULL) {
137 nextll = curll->next;
138 if (curll->object != NULL && objectfree != NULL) {
139 objectfree(curll->object);
140 curll->object = NULL;