-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlist.h
161 lines (142 loc) · 3.72 KB
/
list.h
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
/**
* @file list.h
*
* @author hutusi (hutusi@outlook.com)
*
* @brief Double Linked List.
*
* @date 2019-07-20
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#ifndef RETHINK_C_LIST_H
#define RETHINK_C_LIST_H
/**
* @brief The type of a value to be stored in a @ref List.
* (void *) can be changed to int, long, or other types if needed.
*/
typedef void *ListValue;
/**
* A null @ref ListValue.
*/
#define LIST_NULL ((void *)0)
/**
* @brief Definition of a @ref ListNode.
*
*/
typedef struct _ListNode {
ListValue data;
struct _ListNode *prev;
struct _ListNode *next;
} ListNode;
/**
* @brief Definition of a @ref List.
*
*/
typedef struct _List {
struct _ListNode *head;
struct _ListNode *tail;
unsigned int length;
} List;
/**
* @brief Allcate a new List.
*
* @return List* The new List if success, otherwise return NULL.
*/
List *list_new();
/**
* @brief Delete a List and free back memory.
*
* @param list The list to delete.
*/
void list_free(List *list);
/**
* @brief Prepend a value to the beginning of a List.
*
* @param list The list.
* @param data The value to prepend.
* @return int 0 if success.
*/
int list_prepend(List *list, ListValue data);
/**
* @brief Append a value to the end of a List.
*
* @param list The list.
* @param data The value to append.
* @return int 0 if success.
*/
int list_append(List *list, ListValue data);
/**
* @brief Get the nth node of a List.
*
* @param list The list.
* @param n The nth index.
* @return ListNode* The nth node if success, otherwise return NULL.
*/
ListNode *list_nth_node(List *list, unsigned int n);
/**
* @brief Get the nth node value of a List.
*
* @param list The list.
* @param n The nth index.
* @return ListNode* The nth node value if success, otherwise return LIST_NULL.
*/
ListValue list_nth_data(List *list, unsigned int n);
/**
* @brief Convert an List to an array.
*
* The List still exists!
*
* @param list The List.
* @return ListValue* The ListValue array if success, otherwise return NULL.
*/
ListValue *list_to_array(List *list);
typedef int (*ListCompareFunc)(ListValue value1, ListValue value2);
typedef int (*ListEqualFunc)(ListValue value1, ListValue value2);
/**
* @brief Find a ListNode in a List.
*
* @param list The List.
* @param node The ListNode to lookup.
* @return ListNode* The ListNode if success, otherwise return NULL.
*/
ListNode *list_find_node(List *list, ListNode *node);
/**
* @brief Find the ListNode of a value in a List.
*
* @param list The List.
* @param data The ListNode's value to lookup.
* @return ListNode* The ListNode if success, otherwise return NULL.
*/
ListNode *list_find_data(List *list, ListEqualFunc callback, ListValue data);
/**
* @brief Remove a ListNode from a List.
*
* The ListNode still exists, not be freed.
*
* @param list The List.
* @param node The ListNode.
* @return ListNode* The ListNode if success, otherwise return NULL.
*/
ListNode *list_remove_node(List *list, ListNode *node);
/**
* @brief Remove a ListNode of a value from a List.
*
* The ListNode is be deleted and freed back memory.
*
* @param list The List.
* @param callback The equal callback function.
* @param data The ListNode value.
* @return int 0 if success.
*/
int list_remove_data(List *list, ListEqualFunc callback, ListValue data);
/**
* @brief Sort a List.
*
* @param list The List.
* @param compare_func The compare function to callback.
* @return int 0 if success.
*/
int list_sort(List *list, ListCompareFunc compare_func);
#endif /* #ifndef RETHINK_C_LIST_H */