2 * ll.c - various things of used for dealing with linked lists.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
15 struct ll *lladd(struct ll *curll, void *object)
19 if ((new = malloc(sizeof(struct ll))) == NULL) {
21 printf("Got NULL in lladd()\n");
31 struct ll *lladdend(struct ll *curll, void *object)
36 if ((new = malloc(sizeof(struct ll))) == NULL) {
37 logthing(LOGTHING_ERROR,
38 "Couldn't allocate memory in lladdend()");
47 while (cur->next != NULL) {
58 struct ll *lldel(struct ll *curll, void *object,
59 int (*objectcmp) (const void *object1, const void *object2))
61 struct ll *cur = NULL;
62 struct ll *old = NULL;
64 log_assert(objectcmp != NULL);
69 } else if (!(*objectcmp)(cur->object, object)) {
75 while (cur->next != NULL) {
76 if (!(*objectcmp)(cur->next->object, object)) {
78 cur->next = cur->next->next;
86 struct ll *llfind(struct ll *curll, void *object,
87 int (*objectcmp) (const void *object1, const void *object2))
91 log_assert(objectcmp != NULL);
94 while (cur != NULL && (*objectcmp)(cur->object, object)) {
100 unsigned long llsize(struct ll *curll)
102 unsigned long count = 0;
104 while (curll != NULL) {
113 * llfree - Frees a linked list.
114 * @curll: The list to free.
115 * @objectfree: A pointer to a free function for the object.
117 * Walks through a list and free it. If a function is provided for
118 * objectfree then it's called for each element to free them, if it's NULL
119 * just the list is freed.
121 void llfree(struct ll *curll, void (*objectfree) (void *object))
125 while (curll != NULL) {
126 nextll = curll->next;
127 if (curll->object != NULL && objectfree != NULL) {
128 objectfree(curll->object);
129 curll->object = NULL;