-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.h
More file actions
83 lines (76 loc) · 5.21 KB
/
Copy pathList.h
File metadata and controls
83 lines (76 loc) · 5.21 KB
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
#ifndef LIST_H_
#define LIST_H_
// Basic generic Linked List
#include "stdlib.h"
#include <stddef.h>
#define _LIST_DECL(T, ALIAS,...) \
struct List_##ALIAS; \
typedef T _List_##ALIAS##_T; \
typedef struct List_##ALIAS* List_##ALIAS; \
_List_##ALIAS##_T* \
List_##ALIAS##_next(struct List_##ALIAS ** const node); \
_List_##ALIAS##_T* \
List_##ALIAS##_push(struct List_##ALIAS **node, _List_##ALIAS##_T val); \
_List_##ALIAS##_T* \
List_##ALIAS##_insert(struct List_##ALIAS **node, _List_##ALIAS##_T val); \
_List_##ALIAS##_T List_##ALIAS##_pop(struct List_##ALIAS **node); \
void List_##ALIAS##_append(struct List_##ALIAS **a, \
struct List_##ALIAS *b); \
size_t List_##ALIAS##_length(struct List_##ALIAS ** const node);
#define LIST_IMPL(ALIAS) \
struct List_##ALIAS \
{ \
_List_##ALIAS##_T val; \
struct List_##ALIAS *next; \
}; \
_List_##ALIAS##_T* \
List_##ALIAS##_next(struct List_##ALIAS ** const node) \
{ \
if(!*node) return NULL; \
_List_##ALIAS##_T* res = &(*node)->val; \
*node = (*node)->next; \
return res; \
} \
_List_##ALIAS##_T* \
List_##ALIAS##_push(struct List_##ALIAS **node, _List_##ALIAS##_T val) \
{ \
struct List_##ALIAS* res = malloc(sizeof(struct List_##ALIAS)); \
if(!res) return NULL; \
*res = (struct List_##ALIAS){val, *node}; \
*node = res; \
return &res->val; \
} \
void List_##ALIAS##_append(struct List_##ALIAS **a, \
struct List_##ALIAS *b) \
{ \
struct List_##ALIAS **ptr = a; \
while(*ptr) ptr = &(*ptr)->next; \
*ptr = b; \
} \
_List_##ALIAS##_T* \
List_##ALIAS##_insert(struct List_##ALIAS **node, _List_##ALIAS##_T val) \
{ \
struct List_##ALIAS* res = malloc(sizeof(struct List_##ALIAS)); \
if(!res) return NULL; \
*res = (struct List_##ALIAS){val, NULL}; \
List_##ALIAS##_append(node, res); \
return &res->val; \
} \
_List_##ALIAS##_T \
List_##ALIAS##_pop(struct List_##ALIAS** node) \
{ \
_List_##ALIAS##_T res = (*node)->val; \
struct List_##ALIAS* next = (*node)->next; \
free(*node); \
*node = next; \
return res; \
} \
size_t List_##ALIAS##_length(struct List_##ALIAS ** const node) \
{ \
size_t res = 0; \
List_##ALIAS iter = *node; \
while(List_##ALIAS##_next(&iter)) res++; \
return res; \
}
#define LIST_DECL(...) _LIST_DECL(__VA_ARGS__, __VA_ARGS__,)
#endif // LIST_H_