This repository has been archived by the owner on Dec 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist.h
150 lines (138 loc) · 4.46 KB
/
list.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdbool.h>
#include <stdio.h>
// Linked list macro
// Contains the list, list nodes and iterator
#define DEFINE_LIST(type) \
typedef struct list_node_##type { \
struct list_node_##type *next; \
type *data; \
} list_node_##type ; \
typedef struct list_##type { \
list_node_##type *head; \
list_node_##type *tail; \
} list_##type; \
typedef struct list_iterator_##type { \
list_node_##type *prev; \
list_node_##type *current; \
list_##type* container; \
} list_iterator_##type;
#define LIST_PROTOTYPE(type) \
DEFINE_LIST(type) \
void list_##type##_insert_after(list_node_##type *node, type *data);\
void list_##type##_append(list_##type * container, type* data); \
type* list_##type##_pop(list_##type* container); \
void list_##type##_iterator_init(list_iterator_##type* it, list_##type* container); \
type* list_##type##_next(list_iterator_##type* it); \
type* list_##type##_examine(list_iterator_##type* it); \
type* list_##type##_remove(list_iterator_##type* it); \
bool list_##type##_empty(list_##type* container); \
int list_##type##_length(list_##type* container); \
void list_##type##_sort(list_##type* container, int ( * comparator ) ( const type **, const type ** ));
#define LIST_INSERT_AFTER(type) \
void list_##type##_insert_after(list_node_##type *node, type *data) { \
list_node_##type* newnode = malloc(sizeof(*newnode)); \
newnode->data= data; \
list_node_##type* nextnode = node->next; \
newnode->next = nextnode; \
node->next = newnode; \
}
#define LIST_APPEND(type) \
void list_##type##_insert_first(list_##type* container, type* data){ \
list_node_##type* newnode = malloc(sizeof(*newnode)); \
newnode->data= data; \
newnode->next=NULL; \
container->head = newnode; \
container->tail = newnode; \
} \
void list_##type##_append(list_##type * container, type* data) { \
if(container->tail==NULL)\
list_##type##_insert_first(container,data);\
else {\
list_##type##_insert_after(container->tail, data); \
container->tail=container->tail->next; \
}\
}
#define LIST_POP(type) \
type* list_##type##_pop(list_##type* container){ \
if(container->head==NULL) return NULL; \
list_node_##type* first = container->head;\
container->head=first->next; \
type* data = first->data; \
free(first); \
\
if(container->head==NULL) container->tail=NULL; \
return data;\
}
#define LIST_ITERATOR(type) \
void list_##type##_iterator_init(list_iterator_##type* it, list_##type* container){ \
it->prev= NULL; \
it->current = container->head; \
it->container = container; \
} \
\
type* list_##type##_next(list_iterator_##type* it){\
if(it->current==NULL) return NULL; \
it->prev= it->current; \
it->current = it->current->next; \
return list_##type##_examine(it); \
} \
type* list_##type##_examine(list_iterator_##type* it){\
if(it->current!=NULL) \
return it->current->data; \
return NULL; \
} \
type* list_##type##_remove(list_iterator_##type* it){\
if(it->current==NULL) return NULL; \
if(it->prev==NULL) \
return list_##type##_pop(it->container); \
list_node_##type* temp = it->current; \
it->prev->next = it->current->next; \
it->current = it->current->next; \
\
type* data = temp->data; \
free(temp); \
return data; \
}\
#define LIST_STATUS(type) \
bool list_##type##_empty(list_##type* container){ \
if(container->head==NULL) \
return true; \
return false; \
} \
int list_##type##_length(list_##type* container){ \
list_node_##type * current= container->head; \
int length=0; \
while(current!=NULL){ \
current=current->next; \
length++; \
} \
return length; \
}
#define LIST_SORT(type) \
void list_##type##_sort(list_##type* container, int ( * comparator ) ( const type **, const type ** )){ \
int length = list_##type##_length(container); \
\
type** data = malloc(sizeof(*data)*length); \
type* current = NULL; \
{ \
int i=0; \
while((current=list_##type##_pop(container))!=NULL){ \
data[i++] = current; \
} \
} \
\
typedef int (*compfn)(const void*, const void*); \
qsort (data, length, sizeof(*data), (compfn)(comparator)); \
\
for(int i=0;i<length;i++) \
list_##type##_append(container, data[i]); \
\
free(data); \
}
#define LIST(type) \
LIST_INSERT_AFTER(type) \
LIST_APPEND(type) \
LIST_POP(type) \
LIST_ITERATOR(type) \
LIST_STATUS(type) \
LIST_SORT(type)