summaryrefslogtreecommitdiffstats
path: root/list.c
blob: a05aab2de892ac53973536d6e4e5d2624d81f56c (plain) (blame)
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
#include "list.h"

struct _list {
    LINKEDLIST;
};

void list_init(struct list *list) {
    list->tail = list;
    list->head = list;
    list->end = list;
}

int list_empty(struct list *list) {
    return list->head == list->end;
}

int list_singleton(struct list *list) {
    return list->head == list->tail && list->head != list->end;
}

void *list_circular_prev(void *_node) {
    struct _list *node = _node;
    do { node = node->prev; } while (node == node->listhead);
    return node;
}

void *list_circular_next(void *_node) {
    struct _list *node = _node;
    do { node = node->next; } while (node == node->listhead);
    return node;
}

void list_insert(void *_next, void *_node) {
    struct _list *node = _node;
    struct _list *next = _next;
    struct _list *prev = next->prev;
    next->prev = node;
    prev->next = node;
    node->next = next;
    node->prev = prev;
    node->listhead = next->listhead;
}

void list_remove(void *_node) {
    struct _list *node = _node;
    struct _list *next = node->next;
    struct _list *prev = node->prev;
    next->prev = prev;
    prev->next = next;
}