-
Notifications
You must be signed in to change notification settings - Fork 20
/
list.cpp
224 lines (171 loc) · 2.9 KB
/
list.cpp
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
/*
* Copyright (c) 2011-2013 by naehrwert
* This file is released under the GPLv2.
*/
#include <stdlib.h>
#include "types.h"
#include "list.h"
list_t *list_create()
{
list_t *res;
if((res = (list_t *)malloc(sizeof(list_t))) == NULL)
return NULL;
res->head = NULL;
res->count = 0;
return res;
}
void list_destroy(list_t *l)
{
if(l == NULL)
return;
lnode_t *iter = l->head, *tmp;
while(iter != NULL)
{
tmp = iter;
iter = iter->next;
free(tmp);
}
free(l);
}
BOOL list_isempty(list_t *l)
{
if(l == NULL)
return FALSE;
if(l->count == 0)
return TRUE;
return FALSE;
}
u32 list_count(list_t *l)
{
if(l == NULL)
return 0;
return l->count;
}
BOOL list_push(list_t *l, void *value)
{
if(l == NULL)
return FALSE;
lnode_t *_new;
//Allocate new node.
if((_new = (lnode_t *)malloc(sizeof(lnode_t))) == NULL)
return FALSE;
//Insert.
_new->value = value;
_new->next = l->head;
l->head = _new;
l->count++;
return TRUE;
}
void *list_pop(list_t *l)
{
if(l == NULL)
return NULL;
lnode_t *tmp;
void *res = NULL;
if(l->head != NULL)
{
res = l->head->value;
tmp = l->head;
l->head = l->head->next;
free(tmp);
l->count--;
}
return res;
}
BOOL list_add_back(list_t *l, void *value)
{
if(l == NULL)
return FALSE;
lnode_t *n, *_new;
//Allocate new node.
if((_new = (lnode_t *)malloc(sizeof(lnode_t))) == NULL)
return FALSE;
_new->value = value;
_new->next = NULL;
if(l->head == NULL)
l->head = _new;
else
{
//Move to the list end.
for(n = l->head; n->next != NULL; n = n->next);
//Add.
n->next = _new;
l->count++;
}
return TRUE;
}
void *list_get(list_t *l, u32 idx)
{
if(l == NULL)
return NULL;
lnode_t *iter;
void *res = NULL;
for(iter = l->head; idx-- != 0 && iter != NULL; iter = iter->next);
if(iter == NULL)
res = NULL;
else
res = iter->value;
return res;
}
lnode_t *list_get_node(list_t *l, u32 idx)
{
if(l == NULL)
return NULL;
lnode_t *iter;
for(iter = l->head; idx-- != 0 && iter != NULL; iter = iter->next);
return iter;
}
BOOL list_remove_node(list_t *l, lnode_t *node)
{
if(l == NULL)
return FALSE;
lnode_t *iter;
if(l->head == node)
{
l->head = l->head->next;
free(node);
l->count--;
return TRUE;
}
iter = l->head;
while(iter->next != NULL)
{
if(iter->next == node)
{
iter->next = iter->next->next;
free(node);
l->count--;
return TRUE;
}
iter = iter->next;
}
return FALSE;
}
BOOL list_remove_value(list_t *l, void *value)
{
if(l == NULL)
return FALSE;
lnode_t *tmp, *iter;
if(l->head->value == value)
{
tmp = l->head;
l->head = l->head->next;
free(tmp);
l->count--;
return TRUE;
}
iter = l->head;
while(iter->next != NULL)
{
if(iter->next->value == value)
{
tmp = iter->next;
iter->next = iter->next->next;
free(tmp);
l->count--;
return TRUE;
}
iter = iter->next;
}
return FALSE;
}