-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
87 lines (80 loc) · 1.47 KB
/
Copy pathlist.c
File metadata and controls
87 lines (80 loc) · 1.47 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
#include "list.h"
void list_init(list_t *list)
{
list->head = 0;
list->tail = 0;
}
void list_enqueue(list_t *list, list_node_t *data)
{
if (list->head == 0)
{
list->head = data;
list->tail = data;
data->next = 0;
}
else
{
list->tail->next = data;
list->tail = data;
data->next = 0;
}
}
list_node_t *list_dequeue(list_t *list)
{
list_node_t *data = list->head;
if (data != 0)
{
list->head = data->next;
if (list->head == 0)
{
list->tail = 0;
}
}
return data;
}
void list_remove(list_t *list, list_node_t *node)
{
if (node == list->head)
{
list->head = node->next;
}
if (node == list->tail)
{
list->tail = node->prev;
}
if (node->prev != 0)
{
node->prev->next = node->next;
}
if (node->next != 0)
{
node->next->prev = node->prev;
}
}
list_node_t *list_find(list_t *list, int (*predicate)(const void *))
{
list_node_t *node = list->head;
while (node != 0)
{
if (predicate(node->data))
{
return node;
}
node = node->next;
}
return 0;
}
void list_foreach(list_t *list, void (*func)(list_node_t *))
{
list_node_t *node = list->head;
while (node != 0)
{
list_node_t *next = node->next;
func(node);
node = next;
}
}
int list_is_empty(list_t *list)
{
return list->head == 0;
}