Repository navigation
Expand file tree
/
Copy pathlist.h
More file actions
354 lines (293 loc) · 8.04 KB
/
Copy pathlist.h
File metadata and controls
354 lines (293 loc) · 8.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* ================================================
*
* Copyright 2017-2025 Manoel Vilela
*
* Author: Manoel Vilela
* Contact: manoel_vilela@engineer.com
* Organization: UFC
*
* ===============================================
*/
#ifndef LIST_H
#define LIST_H
#include "../../iterator/iterator.h"
/**
* @brief A node in a singly linked list.
*
* Each node contains an integer key, an integer data field, and a pointer to
* the next node in the list.
*/
struct ListNode {
int key; // optional field, introduced because hash tables
int data;
struct ListNode *next;
};
/**
* @brief A singly linked list.
*
* A `List` is a pointer to the first node in the list. An empty list is
* represented by a null pointer.
*/
typedef struct ListNode List;
#define EMPTY_LIST (List*) 0
/******************/
/* PUBLIC METHODS */
/******************/
/**
* @brief create a new list instance
* @ingroup DataStructureMethods
*/
List* list_create(void);
/**
* @brief Insert a new element on the beginning of the list
* @param l List to insert \p data on
* @param data integer value to insert on
* @return the updated list
* @ingroup DataStructureMethods
*/
List* list_insert(List *l, int data);
/**
* @brief Insert a new element on the beginning of the list
* @param l List to insert \p data and \p key on
* @param data integer value to insert on
* @param key integer value to insert on
* @return the updated list
* @ingroup DataStructureMethods
*/
List* list_insert_with_key(List *l, int key, int data);
/**
* @brief Insert a new element on the end of the list
* @param l List to insert \p data on
* @param data integer value to insert on
* @return the updated list
* @ingroup DataStructureMethods
*/
List* list_append(List *l, int data);
/**
* @brief Insert a new element on the end of the list with key
* @param l List to insert \p data on with \p key
* @param data integer value to insert on
* @param key integer value to insert on
* @return the updated list
* @ingroup DataStructureMethods
*/
List* list_append_with_key(List *l, int key, int data);
/**
* @brief Insert a new node on the end of the list
* @param l List to insert \p data on
* @param node to insert on
* @return the updated list
* @ingroup DataStructureMethods
*/
List* list_append_node(List *l, struct ListNode *node);
/**
* @brief Ordered insert of a new element in the list
* @param l List to insert \p data on
* @param data integer value to insert on
* @return the updated list
* @ingroup DataStructureMethods
*/
List* list_insert_ord(List *l, int data);
/**
* @brief Search on the list by data and return the node which contains it
* @param l List to insert \p data on
* @param data integer value to search on
* @return the node to return it
* @ingroup DataStructureMethods
*/
List* list_search(List *l, int data);
/**
* @brief Search on the list by key and return the node which contains it
* @param l List to search \p key
* @param key integer value to search
* @return the node to return it
* @ingroup DataStructureMethods
*/
List* list_search_by_key(List *l, int key);
/**
* @brief Print the list without a new line
* @param l the list to print
* @ingroup DataStructureMethods
*/
void list_print(List *l);
/**
* @brief Print the list with a new line
* @param l the list to print
* @ingroup DataStructureMethods
*/
void list_println(List *l);
/**
* @brief Print the list reversed without a new line
* @param l the list to print
* @ingroup DataStructureMethods
*/
void list_print_reverse(List *l);
/**
* @brief Print the list reversed with a new line
* @param l the list to print
* @ingroup DataStructureMethods
*/
void list_println_reverse(List *l);
/**
* @brief Remove specific element from List
* @param l List to remove \p data on
* @param data integer value to remove
* @return new list without the node which contains \p data
* @ingroup DataStructureMethods
*/
List* list_remove(List *l, int data);
/**
* @brief Remove specific element from List by a key
* @param l List to remove with \p key
* @param key integer value to remove
* @return new list without the node which contains \p key
* @ingroup DataStructureMethods
*/
List* list_remove_by_key(List *l, int key);
/**
* @brief Remove specific element from List by a key equal data
* @param l List to remove with \p key
* @param key integer value to remove
* @param data integer value to remove
* @return new list without the node which contains \p key and \p data
* @ingroup DataStructureMethods
*/
List* list_remove_by_key_data(List *l, int key, int data);
/**
* @brief Free memory of List and its nodes
* @ingroup DataStructureMethods
*/
void list_free(List *l);
/**
* @brief Verify if the list is empty
* @return 1 if empty and 0 if is not
* @ingroup DataStructureMethods
*/
int list_empty(List *l);
/**
* @brief Check if two lists are equal
* @ingroup DataStructureMethods
*/
int list_equal(List* l_x, List* l_y);
/**********************/
/* ADDITIONAL METHODS */
/**********************/
/**
* @brief Return the count of perfect numbers on list
* @ingroup DataStructureMethods
*/
int list_perfect(List *l);
/**
* @brief Return the length of the list
* @ingroup DataStructureMethods
*/
int list_length(List *l);
/**
* @brief Return the number of numbers less \p n
* @ingroup DataStructureMethods
*/
int list_less_than(List *l, int n);
/**
* @brief Return the sum of numbers on the list
* @ingroup DataStructureMethods
*/
int list_sum(List *l);
/**
* @brief Create a copy of the list l
* @ingroup DataStructureMethods
*/
List* list_copy(List *l);
/**
* @brief Return a concatenation of the two lists as a new list
* @ingroup DataStructureMethods
*/
List* list_concat(List *l_x, List* l_y);
/****************/
/* UTIL METHODS */
/****************/
/**
* @brief Create a list based on its variadic arguments
* @param size_list number of paramaters passed to the function
* @ingroup DataStructureMethods
*/
List* list_init(int size_list, ...);
/**
* @brief Get the data from last element
* @ingroup DataStructureMethods
*/
int list_last(List *l);
/**
* @brief Get the data from the first element
* @ingroup DataStructureMethods
*/
int list_head(List *l);
/**
* @brief Get the tail of the list
* @ingroup DataStructureMethods
*/
List* list_tail(List *l);
/**
* @brief Get and pop the head of the list
* @ingroup DataStructureMethods
*/
int list_pop_head(List** l);
/**
* @brief Get and pop the last element of the list
* @ingroup DataStructureMethods
*/
int list_pop_last(List** l);
/**
* @brief Reverse a list (no creating a new)
* WARNING: side-effects
* @ingroup DataStructureMethods
*/
void list_reverse(List** l);
/*******************/
/* PRIVATE METHODS */
/*******************/
/**
* @brief Check if a given number is perfect
* @ingroup DataStructureMethods
*/
int list__is_perfect_number(int n);
/**
* @brief Create a new node for the list
* @ingroup DataStructureMethods
*/
List* list__new_node(int data);
/**
* @brief Creates an iterator for the list keys.
*
* @param l The list to iterate over.
* @return A pointer to the new iterator.
*/
Iterator* list_iterator_keys(List *l);
/**
* @brief Creates an iterator for the list data.
*
* @param l The list to iterate over.
* @return A pointer to the new iterator.
*/
Iterator* list_iterator_data(List *l);
/**
* @brief Creates an iterator of list nodes.
*
* @param l The list to iterate over.
* @return A pointer to the new iterator.
*/
Iterator* list_iterator(List *l);
/**
* @brief Creates a list from an iterator.
*
* @param it The iterator to create the list from.
* @return A pointer to the new list.
*/
List* list_from_iterator(Iterator* it);
/**
* @brief Sort a list using insertion sort.
*
* @param list Double pointer to the list.
*/
void list_sort(List** list);
#endif