cscvs to tla changeset 77
[onak.git] / ll.c
1 /*
2  * ll.c - various things of used for dealing with linked lists.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2000-2002 Project Purple
7  *
8  * $Id: ll.c,v 1.4 2003/06/04 20:57:10 noodles Exp $
9  */
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include "ll.h"
16
17 struct ll *lladd(struct ll *curll, void *object)
18 {
19         struct ll *new;
20
21         if ((new = malloc(sizeof(struct ll))) == NULL) {
22                 perror("lladd()");
23                 printf("Got NULL in lladd()\n");
24                 return NULL;
25         }
26
27         new->next = curll;
28         new->object = object;
29
30         return new;
31 }
32
33 struct ll *lldel(struct ll *curll, void *object,
34         int (*objectcmp) (const void *object1, const void *object2))
35 {
36         struct ll *cur = NULL;
37         struct ll *old = NULL;
38
39         assert(objectcmp != NULL);
40
41         cur = curll;
42         if (cur == NULL) {
43                 return NULL;
44         } else if (!(*objectcmp)(cur->object, object)) {
45                 old = cur;
46                 cur = cur->next;
47                 free(old);
48                 return cur;
49         } 
50         while (cur->next != NULL) {
51                 if (!(*objectcmp)(cur->next->object, object)) {
52                         old = cur->next;
53                         cur->next = cur->next->next;
54                         free(old);
55                         break;
56                 }
57         }
58         return curll;
59 }
60
61 struct ll *llfind(struct ll *curll, void *object,
62         int (*objectcmp) (const void *object1, const void *object2))
63 {
64         struct ll *cur;
65
66         assert(objectcmp != NULL);
67
68         cur = curll;
69         while (cur != NULL && (*objectcmp)(cur->object, object)) {
70                 cur = cur->next;
71         }
72         return cur;
73 }
74
75 unsigned long llsize(struct ll *curll)
76 {
77         unsigned long count = 0;
78
79         while (curll != NULL) {
80                 count++;
81                 curll = curll->next;
82         }
83
84         return count;
85 }
86
87 /**
88  *      llfree - Frees a linked list.
89  *      @curll: The list to free.
90  *      @objectfree: A pointer to a free function for the object.
91  *
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.
95  */
96 struct ll *llfree(struct ll *curll,
97         void (*objectfree) (void *object))
98 {
99         struct ll *nextll;
100
101         while (curll != NULL) {
102                 nextll = curll->next;
103                 if (curll->object != NULL && objectfree != NULL) {
104                         objectfree(curll->object);
105                         curll->object = NULL;
106                 }
107                 free(curll);
108                 curll = nextll;
109         }
110         return NULL;
111 }