2 * ll.c - various things of used for dealing with linked lists.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
8 * $Id: ll.c,v 1.5 2003/10/04 10:21:41 noodles Exp $
17 struct ll *lladd(struct ll *curll, void *object)
21 if ((new = malloc(sizeof(struct ll))) == NULL) {
23 printf("Got NULL in lladd()\n");
33 struct ll *lldel(struct ll *curll, void *object,
34 int (*objectcmp) (const void *object1, const void *object2))
36 struct ll *cur = NULL;
37 struct ll *old = NULL;
39 assert(objectcmp != NULL);
44 } else if (!(*objectcmp)(cur->object, object)) {
50 while (cur->next != NULL) {
51 if (!(*objectcmp)(cur->next->object, object)) {
53 cur->next = cur->next->next;
61 struct ll *llfind(struct ll *curll, void *object,
62 int (*objectcmp) (const void *object1, const void *object2))
66 assert(objectcmp != NULL);
69 while (cur != NULL && (*objectcmp)(cur->object, object)) {
75 unsigned long llsize(struct ll *curll)
77 unsigned long count = 0;
79 while (curll != NULL) {
88 * llfree - Frees a linked list.
89 * @curll: The list to free.
90 * @objectfree: A pointer to a free function for the object.
92 * Walks through a list and free it. If a function is provided for
93 * objectfree then it's called for each element to free them, if it's NULL
94 * just the list is freed.
96 void llfree(struct ll *curll, void (*objectfree) (void *object))
100 while (curll != NULL) {
101 nextll = curll->next;
102 if (curll->object != NULL && objectfree != NULL) {
103 objectfree(curll->object);
104 curll->object = NULL;