-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.c
More file actions
61 lines (55 loc) · 976 Bytes
/
Copy pathList.c
File metadata and controls
61 lines (55 loc) · 976 Bytes
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
#include "List.h"
void cons(void* info, TList** head) {
TList* new_cell = malloc(sizeof(TList));
if (!new_cell) {
return;
}
new_cell->info = info;
new_cell->next = *head;
*head = new_cell;
}
void destroy(TList** head, TFree destroy) {
while (*head != NULL) {
TList* tmp = *head;
*head = (*head)->next;
destroy(tmp->info);
free(tmp);
}
}
void* head(TList* list) {
if (list != NULL) {
return list->info;
}
return NULL;
}
void remove_elem(TList** list, void* data, eq f, TFree destroy) {
void* a = (*list)->info;
TList* aux;
if (f(data, a)) {
aux = *list;
*list = aux->next;
destroy(aux->info);
free(aux);
return;
}
TList* pred = *list;
aux = pred->next;
while (aux) {
if (f(aux->info, data)) {
pred->next = aux->next;
destroy(aux->info);
free(aux);
return;
}
pred = pred->next;
aux = aux->next;
}
}
int size(TList* list) {
int size = 0;
while (list != NULL) {
size++;
list = list->next;
}
return size;
}