using linked lists instead of arrays
[libradsec.git] / list.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "list.h"
4
5 /* allocates and initialises list structure; returns NULL if malloc fails */
6 struct list *list_create() {
7     struct list *list = malloc(sizeof(struct list));
8     if (list)
9         memset(list, 0, sizeof(struct list));
10     return list;
11 }
12
13 /* frees all memory associated with the list */
14 void list_destroy(struct list *list) {
15     struct list_node *node, *next;
16     
17     for (node = list->first; node; node = next) {
18         free(node->data);
19         next = node->next;
20         free(node);
21     }
22     free(list);
23 }
24
25 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
26 int list_add(struct list *list, void *data) {
27     struct list_node *node;
28
29     node = malloc(sizeof(struct list_node));
30     if (!node)
31         return 0;
32     
33     node->next = NULL;
34     node->data = data;
35
36     if (list->first)
37         list->last->next = node;
38     else
39         list->first = node;
40     list->last = node;
41     
42     return 1;
43 }
44
45 /* returns first node */
46 struct list_node *list_first(struct list *list) {
47     return list->first;
48 }
49
50 /* returns the next node after the argument */
51 struct list_node *list_next(struct list_node *node) {
52     return node->next;
53 }