-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.c
302 lines (264 loc) · 5.22 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (c) 2020-2021 Siddharth Chandrasekaran <sidcha.dev@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdbool.h>
#include "list.h"
void list_init(list_t *list)
{
list->head = NULL;
list->tail = NULL;
}
void list_append(list_t *list, node_t *node)
{
node->prev = list->tail;
node->next = NULL;
if (list->tail) {
list->tail->next = node;
}
list->tail = node;
if (!list->head) {
list->head = node;
}
}
void list_appendleft(list_t *list, node_t *node)
{
node->prev = NULL;
node->next = list->head;
if (list->head) {
list->head->prev = node;
}
list->head = node;
if (!list->tail) {
list->tail = node;
}
}
int list_pop(list_t *list, node_t **node)
{
if (!list->tail) {
return -1;
}
*node = list->tail;
list->tail = list->tail->prev;
if (list->tail) {
list->tail->next = NULL;
}
else {
list->head = NULL;
}
return 0;
}
int list_popleft(list_t *list, node_t **node)
{
if (!list->head) {
return -1;
}
*node = list->head;
list->head = list->head->next;
if (list->head) {
list->head->prev = NULL;
}
else {
list->tail = NULL;
}
return 0;
}
void list_remove_node(list_t *list, node_t *node)
{
if (node->prev == NULL) {
/* remove head */
list->head = node->next;
if (list->head == NULL)
list->tail = NULL;
else
list->head->prev = NULL;
}
else if (node->next == NULL) {
/* remove tail */
list->tail = node->prev;
if (list->tail == NULL)
list->head = NULL;
else
list->tail->next = NULL;
}
else {
/* remove in-between */
node->next->prev = node->prev;
node->prev->next = node->next;
}
}
node_t *list_find_node(list_t *list, node_t *node)
{
node_t *p;
p = list->head;
while (p && p != node)
p = p->next;
return p;
}
bool list_check_links(node_t *p1, node_t *p2)
{
node_t *p1_prev, *p2_next;
if (p1 == NULL || p2 == NULL)
return false;
if (p1 == p2)
return true;
p1_prev = p1->prev;
p2_next = p2->next;
while (p1 && p2 && p1 != p2 && p1->next != p2->prev &&
p1->prev == p1_prev && p2->next == p2_next) {
p1_prev = p1;
p1 = p1->next;
p2_next = p2;
p2 = p2->prev;
}
return (p1 && p2) && (p1 == p2 || p1->next == p2->prev);
}
int list_remove_nodes(list_t *list, node_t *start, node_t *end)
{
/* check if start in list and the range [start:end] is a valid list */
if (list_find_node(list, start) == NULL ||
list_check_links(start, end) == 0)
return -1;
if (start->prev == NULL) {
/* remove from head */
list->head = end->next;
if (list->head == NULL)
list->head = NULL;
else
list->head->prev = NULL;
}
else if (end->next == NULL) {
/* remove upto tail */
start->prev->next = NULL;
list->tail = start->prev;
} else {
/* remove in-between */
start->prev->next = end->next;
end->next->prev = start->prev;
}
return 0;
}
void list_insert_node(list_t *list, node_t *after, node_t *new)
{
node_t *next;
if (after == NULL) {
/* insert at head */
next = list->head;
list->head = new;
} else {
next = after->next;
after->next = new;
}
new->prev = after;
new->next = next;
next->prev = new;
}
int list_insert_nodes(list_t *list, node_t *after, node_t *start, node_t *end)
{
node_t *next;
/* check if nodes is a valid list */
if (list_check_links(start, end) == 0)
return -1;
if (list->head == NULL) {
/* If list is empty, the nodes becomes the list */
list->head = start;
list->tail = end;
}
else if (after == NULL) {
/* if 'after' node is not given prepend the nodes to list */
end->next = list->head;
list->head = start;
list->head->prev = NULL;
}
else {
/* insert nodes into list after the 'after' node */
next = after->next;
after->next = start;
start->prev = after;
end->next = next;
if (next != NULL)
next->prev = end;
else
list->tail = end;
}
return 0;
}
/*--- singly-linked list ---*/
void slist_init(slist_t *list)
{
list->head = NULL;
}
void slist_append(slist_t *list, snode_t *after, snode_t *node)
{
snode_t *p;
p = after ? after : list->head;
if (p == NULL) {
list->head = node;
} else {
while (p && p->next)
p = p->next;
p->next = node;
}
node->next = NULL;
}
void slist_appendleft(slist_t *list, snode_t *node)
{
node->next = list->head;
list->head = node;
}
int slist_pop(slist_t *list, snode_t *after, snode_t **node)
{
snode_t *n1, *n2;
if (list->head == NULL)
return -1;
if (list->head->next == NULL) {
*node = list->head;
list->head = NULL;
} else {
n1 = after ? after : list->head;
n2 = n1->next;
while (n2 && n2->next) {
n1 = n1->next; n2 = n2->next;
}
n1->next = NULL;
*node = n2;
}
return 0;
}
int slist_popleft(slist_t *list, snode_t **node)
{
if (list->head == NULL)
return -1;
*node = list->head;
list->head = list->head->next;
return 0;
}
int slist_remove_node(slist_t *list, snode_t *node)
{
snode_t *prev = NULL, *cur;
cur = list->head;
while (cur && cur != node) {
prev = cur;
cur = cur->next;
}
if (cur == NULL)
return -1;
if (prev == NULL)
list->head = cur->next;
else
prev->next = cur->next;
return 0;
}
void slist_insert_node(slist_t *list, snode_t *after, snode_t *new)
{
if (after == NULL) {
/* same as append left */
new->next = list->head;
list->head = new;
} else {
/* assert after in list here? */
new->next = after->next;
after->next = new;
}
}