-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
executable file
·323 lines (274 loc) · 7.9 KB
/
list.c
File metadata and controls
executable file
·323 lines (274 loc) · 7.9 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* File: lista.c
*
* Define a classe que cria listas duplamente ligadas
*
* Author: sd001 > Bruno Neves, n.º 31614
* > Vasco Orey, n.º 32550
*/
#include "list.h"
#include "list-private.h"
/*
* Cria uma nova lista. Em caso de erro, retorna NULL.
*/
struct list_t *list_create() {
struct list_t *newList = NULL;
if((newList = (struct list_t*)malloc(sizeof(struct list_t)))) {
newList->elements = 0;
newList->head = NULL;
newList->tail = NULL;
}
else {
ERROR("Malloc newList");
}
return newList;
}
/*
* Eliminar uma lista. Desaloca *toda* a memória utilizada pela lista.
* Retorna 0 (OK) ou -1 (erro).
*/
void list_destroy(struct list_t *list) {
struct node_t *tempNode;
if(list) {
//enquanto existir um nó seguinte...
while(list->head) {
tempNode = list->head; //aponta para a cabeça da lista
list->head = list->head->next;//move a cabeça da lista para a frente
node_destroy(tempNode); //destroi o nó
}
free(list);
}
else {
ERROR("Lista NULL");
}
}
/*
* Adicionar um elemento (no final da lista). Retorna 0 (OK) ou -1 (erro).
*/
int list_add(struct list_t *list, struct entry_t *entry) {
int retVal = -1;
struct node_t *newNode;
if(list) {
newNode = node_create(entry);
if(newNode) {
//caso a lista esteja vazia
if(list->elements == 0) {
list->head = newNode;
list->tail = newNode;
}
else {
list->tail->next = newNode;
newNode->prev = list->tail;
list->tail = newNode;
}
list->tail->next = NULL;
list->elements ++;
retVal = 0;
}
else {
ERROR("node_create");
}
}
else {
ERROR("NULL list");
}
return retVal;
}
/*
* Elimina da lista um elemento com a chave key (string). Retorna 0 (OK) ou
* -1 (erro/não encontrado)
*/
int list_remove(struct list_t *list, char *key) {
struct node_t *tempNode;
int found = 0;
if(list) {
tempNode = list->head;
//corre a lista até ao fim ou até ter encontrado a entrada
while(!found && tempNode) {
if(strcmp(tempNode->entry->key, key) == 0) {
found = 1;
}
else {
tempNode = tempNode->next;
}
}
//mudar referências da lista...
if(found) {
//quando o nó a apagar é a cabeça da lista
if(tempNode == list->head) {
list->head = tempNode->next;
}
//quando o nó a apagar é a cauda da lista
else if(tempNode == list->tail) {
list->tail = tempNode->prev;
tempNode->prev->next = NULL;
}
else {
tempNode->prev->next = tempNode->next;
if(tempNode->next) {
tempNode->next->prev = tempNode->prev;
}
}
node_destroy(tempNode);
list->elements --;
}
}
return (found ? 0 : -1);
}
/*
* Obtem um elemento da lista com a chave key (não uma cópia). Retorna a referência
* do elemento na lista (ou seja, uma alteração implica alterar o elemento
* na lista; a função list_remove ou list_destroy vai desalocar a memória
* ocupada pelo elemento, ou NULL em caso de erro).
*/
struct entry_t *list_get(struct list_t *list, char *key) {
struct node_t *tempNode;
struct entry_t *tempEntry = NULL;
int found = 0;
if(list && key) {
tempNode = list->head;
//percorre a lista
while(!found && tempNode && tempNode->entry && tempNode->entry->key) {
if(tempNode->entry->key && strcmp(tempNode->entry->key, key) == 0) {
found = 1;
tempEntry = tempNode->entry;
}
else {
tempNode = tempNode->next;
}
}
}
else {
ERROR("Lista ou key NULL");
}
return tempEntry;
}
/*
* Retorna o tamanho (número de elementos) da lista (-1: erro).
*/
int list_size(struct list_t *list) {
return (list && list->elements >= 0 ? list->elements : -1);
}
/*
* Devolve um array de char * com a cópia de todas as keys da tabela,
* e um último elemento a NULL.
*/
char **list_get_keys(struct list_t *list) {
char **keys = NULL;
struct node_t *tempNode;
int numMallocs = 0, counter = 0;
if(list) {
tempNode = list->head;
// Aloca memória para guardar todas as chaves da tabela
if((keys = (char**)malloc((list->elements + 1)*sizeof(char*)))) {
// Percorre a tabela
while(tempNode) {
// Aloca memória para cada chave e duplica a mesma
if((keys[numMallocs] = strdup(tempNode->entry->key))) {
numMallocs ++;
tempNode = tempNode->next;
}
else {
// Deu erro, temos que libertar tudo o que ja foi allocado !
ERROR("Malloc");
tempNode = NULL;
for(counter = 0; counter < numMallocs; counter ++) {
free(keys[counter]);
}
free(keys);
keys = NULL;
numMallocs = 0;
}
}
// Confirma a cópia de todas as chaves e termina o vector com NULL
if(keys && numMallocs == list->elements) {
keys[numMallocs] = NULL;
}
}
else {
ERROR("Malloc keys");
keys = NULL;
}
}
else {
ERROR("NULL Lista");
}
return keys;
}
/*
* Desaloca a memória alocada por list_get_keys
*/
void list_free_keys(char **keys) {
int counter = 0;
while(keys[counter]) {
free(keys[counter]);
counter++;
}
free(keys);
}
/*
* Retorna todas as entries dessa lista.
*/
struct entry_t **list_get_entries(struct list_t *list) {
struct entry_t **entries = NULL;
struct node_t *node = NULL;
int counter = 0;
if (list) {
// Aloca memória para guardar todas as chaves da tabela
if ((entries = (struct entry_t **) malloc(
sizeof (struct entry_t *) * (list->elements + 1)))) {
node = list->head;
// Percorre a tabela
while (node) {
// Aloca memória para cada entrada e copia-a
if (!(entries[counter] = entry_dup(node->entry))) {
for (counter -= 1; counter >= 0; counter--) {
free(entries[counter]);
}
free(entries);
entries = NULL;
break;
}
else {
node = node->next;
counter++;
}
}
// Confirma a cópia de todas as entradas e termina a NULL
if (entries) {
entries[counter] = NULL;
}
}
}
return entries;
}
/*
* Métodos auxiliares.
*/
/*
* Cria e devolve um novo nó e mantem a referência da entry passada.
* Devolve NULL em caso de erro.
*/
struct node_t *node_create(struct entry_t *entry) {
struct node_t *node = NULL;
if(entry && (node = (struct node_t*) malloc(sizeof(struct node_t)))) {
node->entry = entry;
node->prev = NULL;
node->next = NULL;
}
else {
ERROR("Malloc node or NULL entry");
}
return node;
}
/*
* Destroí o nó passado e desaloca toda a sua memória.
*/
void node_destroy(struct node_t *node) {
if(node) {
if(node->entry) {
entry_destroy(node->entry);
}
free(node);
}
}