forked from NetworkAndSoftware/gopro2gpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
176 lines (137 loc) · 2.86 KB
/
list.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "list.h"
#include <stdlib.h>
// Sun Jul 21 09:34:39 DST 2019
// Copyright Michiel van Wessem
node* list_node(const void* value)
{
node *p = malloc(sizeof(node));
p->value = value;
p->next = 0;
return p;
}
void list_free(node** head)
{
if (head == NULL || *head == NULL)
return;
if ((*head)->next)
list_free(&(*head)->next);
free(*head);
*head = 0;
}
node* list_tail(node* head)
{
if (head == NULL)
return NULL;
while (head->next != NULL)
head = head->next;
return head;
}
node* list_previous(node* n, node* head)
{
if (head == NULL || head == n)
return NULL;
for (; head->next != NULL; head = head->next)
if (head->next == n)
return head;
return NULL;
}
void list_append(node* n, node* head)
{
node* tail = list_tail(head);
if (tail != NULL)
tail->next = n;
}
void list_insert(node* n, node** head)
{
if (head == NULL || *head == NULL || n == NULL)
return;
n->next = *head;
*head = n;
}
int list_find(node* n, node* head)
{
if (n == NULL)
return 0;
for (;head != NULL; head = head->next)
{
if (head == n)
return 1;
}
return 0;
}
int list_remove_one(node* n, node** head)
{
if (head == NULL || *head == NULL || n == NULL)
return 0;
if (*head == n)
{
*head = n->next;
n->next = 0;
return 1;
}
node* previous = list_previous(n, *head);
if (previous == NULL)
return 0;
previous->next = previous->next->next;
n->next = NULL;
return 1;
}
int list_size(node *head)
{
int i = 0;
for (; head != NULL; head = head->next)
i++;
return i;
}
node* list_element_at_index(const int i, node* head)
{
for (int j = 0; head != NULL; head = head->next)
{
if (j == i)
return head;
j++;
}
return NULL;
}
node* list_remove_at_index(const int i, node** head) // returns removed element, so that the caller may free it.
{
if (head == NULL || *head == NULL)
return NULL;
if (i == 0)
{
node* n = *head;
*head = (*head)->next;
return n;
}
const node* previous = list_element_at_index(i - 1, *head);
node* removed = previous->next;
previous->next->next = previous->next;
removed->next = NULL;
return removed;
}
void list_insert_at_index(int i, node* n, node** head)
{
if (head == NULL || *head == NULL)
return;
if (n == 0)
{
n->next = *head;
*head = n;
return;
}
node* previous = list_element_at_index(i - 1, *head);
if (previous == NULL)
return;
n->next = previous->next;
previous->next = n;
}
const void* list_popleft(node** head)
{
if (head == NULL || *head == NULL)
return NULL;
node* node = *head;
const void* value = node->value;
*head = node->next;
free(node);
return value;
}