-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
88 lines (74 loc) · 1.32 KB
/
Copy pathlist.c
File metadata and controls
88 lines (74 loc) · 1.32 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
/*
* Copyright: Sima Alexandru (312CA) 2023
*/
#include <stdlib.h>
#include "list.h"
list_t *encapsulate(void *data)
{
list_t *node = malloc(sizeof(list_t));
if (!node)
return NULL;
node->data = data;
node->prev = NULL;
node->next = NULL;
return node;
}
list_t *last_item(list_t *list)
{
if (!list)
return NULL;
while (list->next)
list = list->next;
return list;
}
void insert_after(list_t **list, list_t *node, list_t *item)
{
if (!node) {
item->next = *list;
item->prev = NULL;
if (*list)
(*list)->prev = item;
*list = item;
return;
}
item->prev = node;
item->next = node->next;
if (item->next)
item->next->prev = item;
node->next = item;
}
void merge_lists(list_t *dest, list_t *source)
{
dest = last_item(dest);
if (!dest || !source)
return;
dest->next = source;
source->prev = dest;
}
void remove_item(list_t **list, list_t *item)
{
list_t *next = item->next;
list_t *prev = item->prev;
if (prev)
prev->next = next;
else
*list = next;
if (next)
next->prev = prev;
}
void clear_list(list_t *list, void (*free_func)(void *))
{
while (list) {
list_t *next = list->next;
free_func(list->data);
free(list);
list = next;
}
}
void apply_func(list_t *list, void (*func)(void *data, void *args), void *args)
{
while (list) {
func(list->data, args);
list = list->next;
}
}