-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
83 lines (67 loc) · 1.53 KB
/
Copy pathlist.c
File metadata and controls
83 lines (67 loc) · 1.53 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
#include "list.h"
#include <stdlib.h>
void list_append(struct list* l, void* data)
{
struct list_node* n = malloc(sizeof(struct list_node));
n->next = NULL;
n->data = data;
if (l->last) l->last->next = n;
else l->first = n;
l->last = n;
}
size_t list_size(struct list* l)
{
size_t i = 0;
for (struct list_node *n = l->first; n; n = n->next)
i++;
return i;
}
void* list_at(struct list* l, size_t a)
{
// if it goes out of bounds = oops
// returning NULL is bad because it can be a valid list node value
// TODO: maybe return list_node instead ?
struct list_node *n = l->first;
for (size_t i = 0; i < a; ++i) {
n = n->next;
}
return n->data;
}
void list_delete(struct list* l, void* who, bool free_data)
{
struct list_node *prev = NULL;
for (struct list_node *n = l->first; n; prev = n, n = n->next) {
if (n->data == who) {
if (prev != NULL) {
prev->next = n->next;
if (prev->next == NULL) l->last = prev;
} else {
l->first = n->next;
if (n->next == NULL) l->last = NULL;
}
if (free_data) free(n->data);
free(n);
return;
}
}
}
void list_delete_nodes(struct list_node* n, bool free_data)
{
if (n == NULL) return;
list_delete_nodes(n->next, free_data);
if (free_data) free(n->data);
free(n);
}
void list_flush(struct list* l, bool free_data)
{
list_delete_nodes(l->first, free_data);
l->first = NULL;
l->last = NULL;
}
struct list_node* list_find(struct list* l, void* data)
{
for (struct list_node *n = l->first; n; n = n->next)
if (n->data == data)
return n;
return NULL;
}