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.4 2003/06/04 20:57:10 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 struct ll *llfree(struct ll *curll,
97 void (*objectfree) (void *object))
101 while (curll != NULL) {
102 nextll = curll->next;
103 if (curll->object != NULL && objectfree != NULL) {
104 objectfree(curll->object);
105 curll->object = NULL;