-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
107 lines (90 loc) · 1.91 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
#include "list.h"
void _list_free(List*,int);
List *
list_new(int (*cmp)(const void*, const void*))
{
List * list = malloc(sizeof(List));
list->head = NULL;
list->cmp = cmp;
list->length = 0;
return list;
}
void
list_add(List * list, void * ele)
{
ListNode * node = malloc(sizeof(ListNode));
node->data = ele;
node->next = list->head;
list->head = node;
list->length++;
}
void *
list_get(List * list, void * ele)
{
ListNode * node = list->head;
while (node != NULL && list->cmp(node->data, ele))
node=node->next;
return (node != NULL) ? node->data : NULL;
}
void *
list_pop(List * list)
{
return list_remove(list, list->head->data);
}
void *
list_remove(List * list, void * ele)
{
ListNode * node = list->head, * prev_node = NULL;
while (node != NULL && list->cmp != NULL && list->cmp(node->data, ele))
{
prev_node = node;
node=node->next;
}
if (node == NULL)
return NULL;
else if (prev_node == NULL)
{
list->head = node->next;
}
else {
prev_node->next = node->next;
}
void * data = node->data;
free(node);
list->length--;
return data;
}
void list_free(List * list)
{
_list_free(list, 0);
}
void list_full_free(List * list)
{
_list_free(list, 1);
}
//
// _list_free - Deallocates all memory associated with the list.
// Can also choose to keep the data blocks in memory, making the user responsible for cleaning
// those blocks;
//
// @param list created by list_new() to be destroyed
// @param do_full_free flag for the user to choose what kind of cleanup he wants.
//
void
_list_free(List * list, int do_full_free)
{
ListNode * node = list->head,* prev_node = NULL;
while (node != NULL)
{
prev_node = node;
node = node->next;
if (do_full_free)
free(prev_node->data);
free(prev_node);
}
free(list);
}
int int_cmp(const void * a, const void * b)
{
return *(int*)a - *(int*)b;
}