-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlist.h
51 lines (43 loc) · 1.93 KB
/
dlist.h
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
/*****************************************************************************
* Notice: *
* This source has been taken from : Algorithmen mit C von Kyle Loudon, *
* O'Reilly & Associates (ISBN: 3-89721-165-3). *
****************************************************************************/
#ifndef DLIST_H
#define DLIST_H
#include <stdlib.h>
typedef struct DListElmt_ {
void *data;
struct DListElmt_ *prev;
struct DListElmt_ *next;
} DListElmt;
typedef struct DList_ {
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
DListElmt *head;
DListElmt *tail;
} DList;
DListElmt *dlist_find(DList *list, const void *data); /* [email protected] */
void dlist_init(DList *list, void (*destroy)(void *data));
void dlist_destroy(DList *list);
int dlist_ins_next(DList *list, DListElmt *element, const void *data);
int dlist_ins_prev(DList *list, DListElmt *element, const void *data);
int dlist_remove(DList *list, DListElmt *element, void **data);
#define dlist_remove_and_destroy(list, data)\
{ void *data2;\
dlist_remove((list), dlist_find((list), (data)), &data2);\
(*(list)).destroy(data2);\
}
#define dlist_size(list) ((list)->size)
#define dlist_head(list) ((list)->head)
#define dlist_tail(list) ((list)->tail)
#define dlist_is_head(element) ((element)->prev == NULL ? 1 : 0)
#define dlist_is_tail(element) ((element)->next == NULL ? 1 : 0)
#define dlist_data(element) ((element)->data)
#define dlist_next(element) ((element)->next)
#define dlist_prev(element) ((element)->prev)
#define FOR_EACH(list, element) for((element) = (list)->head; (element) && (element)->data; (element) = (element)->next)
#define FOR_EACH_DATA(list, obj) { DListElmt* element; FOR_EACH(list, element) { (obj)=(element)->data;
#define END_DATA } }
#endif