-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx_linkedlist.c
More file actions
112 lines (96 loc) · 2.04 KB
/
mx_linkedlist.c
File metadata and controls
112 lines (96 loc) · 2.04 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
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <mx_linkedlist.h>
#include <unistd.h>
/*
* Init the linked list, memory for the list is allready allocated.
*/
bool linked_list_init (linked_list_t *list, list_free_func_t free_func)
{
if (!list) {
return false;
}
memset(list, 0, sizeof(linked_list_t));
list->free_func = free_func;
return true;
}
/*
* Add the data to a linked list.
*/
bool linked_list_add_node (linked_list_t *list,
void *data)
{
list_node_t *curr_node, *node;
if (!list) {
return false;
}
node = malloc(sizeof(list_node_t));
memset(node, 0, sizeof(list_node_t));
node->next = NULL;
node->data = data;
if (!node) {
return false;
}
if (list->head) {
if (!list->recent_node) {
return false;
}
curr_node = list->recent_node;
curr_node->next = node;
} else {
list->head = node;
}
node->next = NULL;
list->recent_node = node;
list->node_cnt++;
return true;
}
/*
* Get the number of nodes in the list.
*/
int linked_list_get_node_count (linked_list_t *list)
{
int cnt = 0;
if (list) {
cnt = list->node_cnt;
}
return cnt;
}
/*
* Destroy the linked list.
*/
bool linked_list_destroy (linked_list_t *list)
{
list_node_t *node = NULL, *tmp_node = NULL;
if (!list) {
return false;
}
node = list->head;
while (node) {
list->free_func(node->data);
tmp_node = node->next;
free(node);
node = tmp_node;
}
linked_list_init(list, NULL);
return true;
}
/*
* Walk over the linked list, call the walk_func call back for every node data.
*/
bool linked_list_walk (linked_list_t *list,
list_walk_func_t walk_func)
{
list_node_t *node = NULL, *tmp_node = NULL;
if (!list) {
return false;
}
node = list->head;
while (node) {
walk_func(node->data);
node = node->next;
}
return true;
}