1
0
Fork 0

More work on single linked lists, added more clearing function.

This commit is contained in:
wael 2022-02-16 16:51:32 +02:00
parent 4c394f94b5
commit 30ad230ade
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
2 changed files with 68 additions and 11 deletions

View File

@ -265,7 +265,7 @@ static inline void remove_node_i_single_linked_list(linked_list_t *list, size_t
}
/* This function deletes the ith node with the data stored within it. */
static inline void remove_node_i_data_single_linked_list(linked_list_t *list, size_t i, void (*free_function)(void*)) {
static inline void remove_node_i_data_single_linked_list(linked_list_t *list, size_t i, void (*free_function)(const void*)) {
/* Input sanity check */
assert(list && list->length > i);
@ -329,7 +329,7 @@ static inline void remove_node_head_single_linked_list(linked_list_t *list) {
}
/* This function deletes the first node and the contents thereof. */
static inline void remove_node_head_data_single_linked_list(linked_list_t *list, void (*free_function)(void*)) {
static inline void remove_node_head_data_single_linked_list(linked_list_t *list, void (*free_function)(const void*)) {
/* Sanity check. */
assert(list && free_function);
@ -366,7 +366,7 @@ static inline void remove_node_tail_single_linked_list(linked_list_t *list) {
}
/* This function deletes the last node and the data thereof. */
static inline void remove_node_tail_data_single_linked_list(linked_list_t *list, void (*free_function)(void*)) {
static inline void remove_node_tail_data_single_linked_list(linked_list_t *list, void (*free_function)(const void*)) {
/* Sanity check. */
assert(list && (list->head));
@ -403,22 +403,22 @@ static inline int clear_list_single_linked_list(linked_list_t **list) {
/* This function deletes all the nodes and data stored within of the given list and the list itself. */
/* Returns 1 on success and 0 on failure, undefined behavior in the latter. Will return -1 when the list was freed successfully but was initially malformed. */
/* If free_function is NULL will use STDLIB free instead. */
static inline int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(void*)) {
static inline int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(const void*)) {
/* Sanity check. */
assert(list && *list);
/* Local variables. */
node_t *current = (*list)->head, *tmp;
size_t length, count = 0;
/* Loop and free. */
while(current) {
count++;
tmp = current->next;
if (free_function) {
if (free_function)
free_function(current->data);
} else {
else
free(current->data);
}
free(current);
current = tmp;
}
@ -431,6 +431,59 @@ static inline int clear_list_data_single_linked_list(linked_list_t **list, void
return (count == length) ? SUCCESS_CODE_SINGLE_LINKED_LIST_C : CODE_FREE_SUCCESS_MALFORMED_SINGLE_LINKED_LIST_C;
}
/* This function deletes all the nodes of a linked list. */
static inline void clear_list_nodes_single_linked_list(linked_list_t *list) {
/* Sanity check. */
assert(list);
/* Local variables. */
node_t *current = list->head, *tmp;
size_t length, count = 0L;
/* Special case. */
if (!(list->length) || !(list->head))
return;
/* Loop and free. */
while (current) {
count++;
tmp = current->next;
free(current);
current = tmp;
}
/* Reset the length. */
list->length = 0L;
}
/* This function deletes all the nodes and the data stored within of a linked list. */
/* If free_function is NULL will use STDLIB free instead. */
static inline void clear_list_nodes_data_single_linked_list(linked_list_t *list, void (*free_function)(const void*)) {
/* Sanity check. */
assert(list);
/* Local variables. */
node_t *current = list->head, *tmp;
size_t length, count = 0L;
/* Special case. */
if (!(list->length) || !(list->head))
return;
/* Loop and free. */
while (current) {
count++;
tmp = current->next;
if (free_function)
free_function(current->data);
else
free(current->data);
free(current);
current = tmp;
}
/* Reset the length. */
list->length = 0L;
}
/* This function attetmps to serialize a linked list into an array. */
/* Note that the array has to be allocated by the caller. */
/* The array setting function is used to set the values at an index, takes the pointer to the array, index, and value to put. */

View File

@ -73,7 +73,7 @@ static inline size_t get_node_index_data_single_linked_list(linked_list_t *list,
/* This function deletes the ith node. */
static inline void remove_node_i_single_linked_list(linked_list_t *list, size_t i);
/* This function deletes the ith node and the data stored within. */
static inline void remove_node_i_data_single_linked_list(linked_list_t *list, size_t i, void (*free_function)(void*));
static inline void remove_node_i_data_single_linked_list(linked_list_t *list, size_t i, void (*free_function)(const void*));
/* This function reads the data at the first node. */
static inline void* read_node_head_single_linked_list(linked_list_t *list);
/* This function retrieves the first node. */
@ -81,7 +81,7 @@ static inline node_t* get_node_head_single_linked_list(linked_list_t *list);
/* This function deletes the first node. */
static inline void remove_node_head_single_linked_list(linked_list_t *list);
/* This function deletes the first node and the data thereof. */
static inline void remove_node_head_data_single_linked_list(linked_list_t *list, void (*free_function)(void*));
static inline void remove_node_head_data_single_linked_list(linked_list_t *list, void (*free_function)(const void*));
/* This function reads the data at the last node. */
static inline void* read_node_tail_single_linked_list(linked_list_t *list);
/* This function retrieves the last node. */
@ -89,11 +89,15 @@ static inline node_t* get_node_tail_single_linked_list(linked_list_t *list);
/* This function deletes the last node. */
static inline void remove_node_tail_single_linked_list(linked_list_t *list);
/* This function deletes the last node and the data thereof. */
static inline void remove_node_tail_data_single_linked_list(linked_list_t *list, void (*free_function)(void*));
static inline void remove_node_tail_data_single_linked_list(linked_list_t *list, void (*free_function)(const void*));
/* This function deletes all the nodes of the given list and the list itself. */
static inline int clear_list_single_linked_list(linked_list_t **list);
/* This function deletes all the nodes and data stored within of the given list and the list itself. */
static inline int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(void*));
static inline int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(const void*));
/* This function deletes all the nodes of a linked list. */
static inline void clear_list_nodes_single_linked_list(linked_list_t *list);
/* This function deletes all the nodes of a linked list and the data thereof. */
static inline void clear_list_nodes_data_single_linked_list(linked_list_t *list, void (*free_function)(const void*));
/* This function attempts to serialize a linked list into an array, it relies on an array setting function and an allocated buffer. */
static inline void *serialize_linked_list(linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array);
#endif /* SINGLE_LINKED_LIST_H */