-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathlist.c
More file actions
131 lines (115 loc) · 3.19 KB
/
Copy pathlist.c
File metadata and controls
131 lines (115 loc) · 3.19 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
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
#include "list.h"
void flist_push_front(s_flist_node **list, s_flist_node *node) {
if ((list != NULL) && (node != NULL)) {
node->next = *list;
*list = node;
}
}
void flist_pop_front(s_flist_node **list, f_list_node_del func) {
s_flist_node *tmp;
if (list != NULL) {
tmp = *list;
if (tmp != NULL) {
*list = tmp->next;
if (func != NULL) func(tmp);
}
}
}
void flist_push_back(s_flist_node **list, s_flist_node *node) {
s_flist_node *tmp;
if ((list != NULL) && (node != NULL)) {
if (*list == NULL) {
*list = node;
} else {
tmp = *list;
if (tmp != NULL) {
for (; tmp->next != NULL; tmp = tmp->next)
;
tmp->next = node;
}
}
}
}
void flist_pop_back(s_flist_node **list, f_list_node_del func) {
s_flist_node *tmp;
if (list != NULL) {
tmp = *list;
if (tmp != NULL) {
// only one element
if (tmp->next == NULL) {
flist_pop_front(list, func);
} else {
for (; tmp->next->next != NULL; tmp = tmp->next)
;
if (func != NULL) func(tmp->next);
tmp->next = NULL;
}
}
}
}
void flist_insert_after(s_flist_node **list, s_flist_node *ref, s_flist_node *node) {
(void) list;
if ((ref != NULL) && (node != NULL)) {
node->next = ref->next;
ref->next = node;
}
}
void flist_remove(s_flist_node **list, s_flist_node *node, f_list_node_del func) {
s_flist_node *it;
s_flist_node *tmp;
if ((list != NULL) && (node != NULL)) {
if (node == *list) {
// first element
flist_pop_front(list, func);
} else {
it = *list;
if (it != NULL) {
for (; it->next != node; it = it->next)
;
tmp = it->next->next;
if (func != NULL) func(it->next);
it->next = tmp;
}
}
}
}
void flist_clear(s_flist_node **list, f_list_node_del func) {
s_flist_node *tmp;
s_flist_node *next;
if (list != NULL) {
tmp = *list;
while (tmp != NULL) {
next = tmp->next;
if (func != NULL) func(tmp);
tmp = next;
}
*list = NULL;
}
}
size_t flist_size(s_flist_node *const *list) {
size_t size = 0;
if (list != NULL) {
for (s_flist_node *tmp = *list; tmp != NULL; tmp = tmp->next) size += 1;
}
return size;
}
void flist_sort(s_flist_node **list, f_list_node_cmp func) {
s_flist_node **tmp;
s_flist_node *a, *b;
bool sorted;
if ((list != NULL) && (func != NULL)) {
do {
sorted = true;
for (tmp = list; (*tmp != NULL) && ((*tmp)->next != NULL); tmp = &(*tmp)->next) {
a = *tmp;
b = a->next;
if (func(a, b) == false) {
*tmp = b;
a->next = b->next;
b->next = a;
sorted = false;
}
}
} while (!sorted);
}
}