-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
68 lines (58 loc) · 1.24 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdlib.h>
#include "list.h"
void olio_list_init(olio_list * l)
{
/* initialize the head and tail */
l->head = l->tail = NULL;
/* count of items */
l->count = 0;
}
void olio_list_free(olio_list * l)
{
olio_list_entry * a;
olio_list_entry * b;
a = l->head;
while (a != NULL) {
b = a->next;
free(a);
a = b;
}
/* reset to initialized state */
l->head = l->tail = NULL;
l->count = 0;
}
int olio_list_append(olio_list * l, void * data)
{
olio_list_entry * a = (olio_list_entry *) malloc(sizeof(olio_list_entry));
if (a == NULL) return -1;
a->data = data;
a->next = NULL;
a->prev = l->tail;
if (l->tail != NULL) l->tail->next = a;
else l->head = a;
l->tail = a;
l->count++;
return 0;
}
void olio_list_head(olio_list * l, void ** opaque)
{
*opaque = l->head;
}
void * olio_list_next(olio_list * l, void ** opaque)
{
olio_list_entry * h = (olio_list_entry *) (*opaque);
if (h == NULL) return NULL;
*opaque = h->next;
return h->data;
}
void olio_list_tail(olio_list * l, void ** opaque)
{
*opaque = l->tail;
}
void * olio_list_prev(olio_list * l, void ** opaque)
{
olio_list_entry * h = (olio_list_entry *) (*opaque);
if (h == NULL) return NULL;
*opaque = h->prev;
return h->data;
}