1
0
Fork 0

Clean up of double linked lists, with expansion of length to size_t instead of int.

This commit is contained in:
wael 2022-01-05 17:47:42 +02:00
parent 87cd5c3f45
commit 48c240315f
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
2 changed files with 106 additions and 109 deletions

View File

@ -37,7 +37,7 @@ double_linked_list_t* initialize_double_linked_list() {
return NULL;
/* Initialize the internal fields. */
list->length = 0;
list->length = 0L;
list->head = NULL;
list->tail = NULL;
@ -49,7 +49,7 @@ double_linked_list_t* initialize_double_linked_list() {
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int append_node_double_linked_list(double_linked_list_t *list, double_node_t *node){
/* Sanity check - make sure input is not NULL and list state is defined! */
if (list == NULL || node == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
if (!list || !node || (list->head && !(list->tail)) || (!(list->head) && list->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Set the next node to NULL. */
@ -57,7 +57,7 @@ int append_node_double_linked_list(double_linked_list_t *list, double_node_t *no
/* The data is in a defined state, append. */
/* Split cases according to if this is the first node or not. */
if (list->head == NULL) {
if (!(list->head)) {
/* Set the previous node to NULL. */
node->previous = NULL;
/* Attach. */
@ -71,7 +71,7 @@ int append_node_double_linked_list(double_linked_list_t *list, double_node_t *no
list->tail->next = node;
node->previous = list->tail;
list->tail = node;
list->length = list->length + 1;
(list->length)++;
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -79,7 +79,7 @@ int append_node_double_linked_list(double_linked_list_t *list, double_node_t *no
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int prepend_node_double_linked_list(double_linked_list_t *list, double_node_t *node){
/* Sanity check - make sure input is not NULL and list state is defined! */
if (list == NULL || node == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
if (!list || !node || (list->head && !(list->tail)) || (!(list->head) && list->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Set the previous node to NULL. */
@ -87,7 +87,7 @@ int prepend_node_double_linked_list(double_linked_list_t *list, double_node_t *n
/* The data is in a defined state, prepend. */
/* Split cases according to if this is the first node or not. */
if (list->head == NULL) {
if (!(list->head)) {
/* Set the next node to NULL. */
node->next = NULL;
/* Attach. */
@ -101,7 +101,7 @@ int prepend_node_double_linked_list(double_linked_list_t *list, double_node_t *n
list->head->previous = node;
node->next = list->head;
list->tail = node;
list->length = list->length + 1;
(list->length)++;
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -109,7 +109,7 @@ int prepend_node_double_linked_list(double_linked_list_t *list, double_node_t *n
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int append_node_data_double_linked_list(double_linked_list_t *list, void *data){
/* Sanity check - make sure input is not NULL and list state is defined! */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
@ -118,7 +118,7 @@ int append_node_data_double_linked_list(double_linked_list_t *list, void *data){
/* Attempt to allocate the node. */
node = (double_node_t*) malloc(sizeof(double_node_t));
/* Check for allocation failure. */
if (node == NULL)
if (!node)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Store the value. */
node->data = data;
@ -131,7 +131,7 @@ int append_node_data_double_linked_list(double_linked_list_t *list, void *data){
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int prepend_node_data_double_linked_list(double_linked_list_t *list, void *data){
/* Sanity check - make sure input is not NULL and list state is defined! */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
@ -140,7 +140,7 @@ int prepend_node_data_double_linked_list(double_linked_list_t *list, void *data)
/* Attempt to allocate the node. */
node = (double_node_t*) malloc(sizeof(double_node_t));
/* Check for allocation failure. */
if (node == NULL)
if (!node)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Store the value. */
node->data = data;
@ -151,24 +151,24 @@ int prepend_node_data_double_linked_list(double_linked_list_t *list, void *data)
/* This function inserts a node at the ith place. */
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int add_node_i_double_linked_list(double_linked_list_t *list, double_node_t *node, int i) {
int add_node_i_double_linked_list(double_linked_list_t *list, double_node_t *node, size_t i) {
/* Sanity check - make sure input is not NULL, list state is defined and index is valid. */
if (list == NULL || node == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL) || i < 0 || i > list->length)
if (!list || !node || (list->head && !(list->tail)) || (!(list->head) && list->tail) || i > list->length)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Deal with special "easy" cases. */
if (i == 0)
if (!i)
return prepend_node_double_linked_list(list, node);
if (i == list->length)
return append_node_double_linked_list(list, node);
/* Local variables. */
int index;
size_t index;
double_node_t* current;
/* Loop until we get to the desired node. */
current = list->head;
for (index = 0; index < i; index++)
for (index = 0L; index < i; index++)
current = current->next;
/* Insert before current. */
node->previous = current->previous;
@ -177,7 +177,7 @@ int add_node_i_double_linked_list(double_linked_list_t *list, double_node_t *nod
current->previous = node;
/* Increase the length. */
list->length = list->length + 1;
(list->length)++;
/* Inserted. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
@ -185,9 +185,9 @@ int add_node_i_double_linked_list(double_linked_list_t *list, double_node_t *nod
/* This function creates and inserts a node at the ith place. */
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int add_node_data_i_double_linked_list(double_linked_list_t *list, void *data, int i) {
int add_node_data_i_double_linked_list(double_linked_list_t *list, void *data, size_t i) {
/* Sanity check - make sure list state is defined and index is valid. */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL) || i < 0 || i > list->length)
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail) || i > list->length)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
@ -196,7 +196,7 @@ int add_node_data_i_double_linked_list(double_linked_list_t *list, void *data, i
/* Attempt to allocate the node. */
node = (double_node_t*) malloc(sizeof(double_node_t));
/* Check for allocation failure. */
if (node == NULL)
if (!node)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Store the data. */
node->data = data;
@ -208,22 +208,22 @@ int add_node_data_i_double_linked_list(double_linked_list_t *list, void *data, i
/* This function reads the data at the ith node. */
/* Returns NULL on failure! (Even if NULL can be a legitimate value). */
/* Sets error to SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
void* read_node_i_double_linked_list(double_linked_list_t *list, int i, int *error) {
void* read_node_i_double_linked_list(double_linked_list_t *list, size_t i, int *error) {
/* Sanity check - make sure list state is defined and index is valid. */
if (error == NULL)
if (!error)
return NULL;
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL) || i < 0 || i > list->length) {
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail) || i > list->length) {
*error = FAILURE_CODE_DOUBLE_LINKED_LIST_C;
return NULL;
}
/* Local variables. */
int index;
size_t index;
double_node_t* current;
/* Loop until we get to the desired node. */
current = list->head;
for (index = 0; index < i; index++)
for (index = 0L; index < i; index++)
current = current->next;
/* Set the error code. */
@ -236,22 +236,22 @@ void* read_node_i_double_linked_list(double_linked_list_t *list, int i, int *err
/* This function retrieves a pointer to the ith node. */
/* Returns NULL on failure! */
/* Sets error to SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
double_node_t* get_node_i_double_linked_list(double_linked_list_t *list, int i, int *error) {
double_node_t* get_node_i_double_linked_list(double_linked_list_t *list, size_t i, int *error) {
/* Sanity check - make sure list state is defined and index is valid. */
if (error == NULL)
if (!error)
return NULL;
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL) || i < 0 || i > list->length) {
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail != NULL) || i > list->length) {
*error = FAILURE_CODE_DOUBLE_LINKED_LIST_C;
return NULL;
}
/* Local variables. */
int index;
size_t index;
double_node_t* current;
/* Loop till we get to the desired node. */
current = list->head;
for (index = 0; index < i; index++)
for (index = 0L; index < i; index++)
current = current->next;
/* Set the error code. */
@ -263,14 +263,14 @@ double_node_t* get_node_i_double_linked_list(double_linked_list_t *list, int i,
/* This function attempts to find the index of a given node, by pointer. */
/* Returns INDEX_NOT_FOUND_CODE_DOUBLE_LINKED_LIST_C on failure. */
int get_node_index_pointer_double_linked_list(double_linked_list_t *list, double_node_t *node) {
size_t get_node_index_pointer_double_linked_list(double_linked_list_t *list, double_node_t *node) {
/* Sanity check. */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL) || node == NULL)
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail) || !node)
return INDEX_NOT_FOUND_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
int index = 0;
size_t index = 0L;
double_node_t* current = list->head;
/* Loop and look for it. */
@ -285,29 +285,29 @@ int get_node_index_pointer_double_linked_list(double_linked_list_t *list, double
/* This function deletes the ith node, leaves the data AS IS! */
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int remove_node_i_double_linked_list(double_linked_list_t *list, int i) {
int remove_node_i_double_linked_list(double_linked_list_t *list, size_t i) {
/* Sanity check - make sure list state is defined and index is valid. */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL) || i < 0 || i > list->length)
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail) || i > list->length)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
double_node_t* current;
int index;
size_t index;
/* Special case of an empty list. */
if (list->length == 0) {
/* Done nothing. */
if (!(list->length)) {
/* Do nothing. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
/* Special case of a list with a single node. */
if (list->length == 1) {
if (list->length == 1L) {
/* Clear the node. */
free(list->head);
/* "Relink" */
list->head = NULL;
list->tail = NULL;
list->length = 0;
list->length = 0L;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -321,7 +321,7 @@ int remove_node_i_double_linked_list(double_linked_list_t *list, int i) {
/* Try to remove the node. */
/* Check if head or tail. */
if (current->previous == NULL) {
if (!(current->previous)) {
/* Update head. */
list->head = current->next;
list->head->previous = NULL;
@ -329,11 +329,11 @@ int remove_node_i_double_linked_list(double_linked_list_t *list, int i) {
free(current);
current = NULL;
/* Update length. */
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
if (current->next == NULL) {
if (!(current->next)) {
/* Update tail. */
list->tail = current->previous;
current->previous->next = NULL;
@ -341,7 +341,7 @@ int remove_node_i_double_linked_list(double_linked_list_t *list, int i) {
free(current);
current = NULL;
/* Update length. */
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -354,7 +354,7 @@ int remove_node_i_double_linked_list(double_linked_list_t *list, int i) {
free(current);
current = NULL;
/* Update length. */
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -362,25 +362,25 @@ int remove_node_i_double_linked_list(double_linked_list_t *list, int i) {
/* This function deletes the ith node and the data stored within. */
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
/* If free_function is NULL, will use the stdlib's free(). */
int remove_node_i_data_double_linked_list(double_linked_list_t *list, int i, void (*free_function)(const void*)) {
int remove_node_i_data_double_linked_list(double_linked_list_t *list, size_t i, void (*free_function)(const void*)) {
/* Sanity check - make sure list state is defined and index is valid. */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL) || i < 0 || i > list->length)
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail) || i > list->length)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
double_node_t* current;
int index;
size_t index;
/* Special case of an empty list. */
if (list->length == 0) {
if (!(list->length)) {
/* Done nothing. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
/* Special case of a list with a single node. */
if (list->length == 1) {
if (list->length == 1L) {
/* Free the data within the node. */
if (free_function == NULL)
if (!free_function)
free(list->head->data);
else
free_function(list->head->data);
@ -389,49 +389,46 @@ int remove_node_i_data_double_linked_list(double_linked_list_t *list, int i, voi
/* "Relink" */
list->head = NULL;
list->tail = NULL;
list->length = 0;
list->length = 0L;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
/* Loop till we get to the desired node. */
while (index < i) {
/* Increment. */
index++;
for (index = 0L; index < i; index++)
current = current->next;
}
/* Try to remove the node. */
/* Check if head or tail. */
if (current->previous == NULL) {
if (!(current->previous)) {
/* Update head. */
list->head = current->next;
current->next->previous = NULL;
/* Free node struct and the data within it. */
if (free_function != NULL)
free_function(current->data);
else
if (!free_function)
free(current->data);
else
free_function(current->data);
free(current);
current = NULL;
/* Update length. */
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
if (current->next == NULL) {
if (!(current->next)) {
/* Update tail. */
list->tail = current->previous;
current->previous->next = NULL;
/* Free node struct and the data within it. */
if (free_function != NULL)
if (!free_function)
free_function(current->data);
else
free(current->data);
free(current);
current = NULL;
/* Update length. */
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -441,14 +438,14 @@ int remove_node_i_data_double_linked_list(double_linked_list_t *list, int i, voi
current->previous->next = current->next;
current->next->previous = current->previous;
/* Free node struct and the data within it. */
if (free_function != NULL)
if (!free_function)
free_function(current->data);
else
free(current->data);
free(current);
current = NULL;
/* Update length. */
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -457,7 +454,7 @@ int remove_node_i_data_double_linked_list(double_linked_list_t *list, int i, voi
/* Returns NULL on error or empty list. */
void* read_node_head_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0 || list->head == NULL)
if (!list || !(list->length) || !(list->head))
return NULL;
/* Return the data. */
@ -468,7 +465,7 @@ void* read_node_head_double_linked_list(double_linked_list_t *list) {
/* Returns NULL on error or empty list. */
double_node_t* get_node_head_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0)
if (!list || !(list->length))
return NULL;
/* Return the data. */
@ -479,21 +476,21 @@ double_node_t* get_node_head_double_linked_list(double_linked_list_t *list) {
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int remove_node_head_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Check if we have to do anything at all. */
if (list->head == NULL)
if (!(list->head))
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
/* Split into cases. */
if (list->length == 1) {
if (list->length == 1L) {
/* Clear the only node. */
free(list->head);
/* Set the list to be empty again. */
list->head = NULL;
list->tail = NULL;
list->length = 0;
list->length = 0L;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -510,7 +507,7 @@ int remove_node_head_double_linked_list(double_linked_list_t *list) {
free(tmp);
/* Change the length. */
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
@ -521,17 +518,17 @@ int remove_node_head_double_linked_list(double_linked_list_t *list) {
/* If free_function is NULL, will use the stdlib's free(). */
int remove_node_head_data_double_linked_list(double_linked_list_t *list, void (*free_function)(const void*)) {
/* Sanity check. */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
if (!list || (list->head && !(list->tail)) || (!(list->head) && list->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Check if we have to do anything at all. */
if (list->head == NULL)
if (!(list->head))
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
/* Split into cases. */
if (list->length == 1) {
if (list->length == 1L) {
/* Clear the only node and the data therof. */
if (free_function == NULL)
if (!free_function)
free(list->head->data);
else
free_function(list->head->data);
@ -539,7 +536,7 @@ int remove_node_head_data_double_linked_list(double_linked_list_t *list, void (*
/* Set the list to be empty again. */
list->head = NULL;
list->tail = NULL;
list->length = 0;
list->length = 0L;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -553,14 +550,14 @@ int remove_node_head_data_double_linked_list(double_linked_list_t *list, void (*
list->head->previous = NULL;
/* Free the node and data thereof. */
if (free_function == NULL)
if (!free_function)
free(list->head->data);
else
free_function(list->head->data);
free(tmp);
/* Change the length. */
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
@ -570,7 +567,7 @@ int remove_node_head_data_double_linked_list(double_linked_list_t *list, void (*
/* Returns NULL on error or empty list. */
void* read_node_tail_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0 || list->tail == NULL)
if (!list || !(list->length) || !(list->tail))
return NULL;
/* Return the data. */
@ -581,7 +578,7 @@ void* read_node_tail_double_linked_list(double_linked_list_t *list) {
/* Returns NULL on error or empty list. */
double_node_t* get_node_tail_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0)
if (!list || !(list->length))
return NULL;
/* Return the data. */
@ -593,7 +590,7 @@ double_node_t* get_node_tail_double_linked_list(double_linked_list_t *list) {
/* Failure is also when the list state isn't properly defined! */
int remove_node_tail_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0 || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
if (!list || !(list->length) || (list->head && !(list->tail)) || (!(list->head) && list->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
@ -601,11 +598,11 @@ int remove_node_tail_double_linked_list(double_linked_list_t *list) {
/* Split cases. */
/* Single node case. */
if (list->length == 1) {
if (list->length == 1L) {
free(list->tail);
list->head = NULL;
list->tail = NULL;
list->length = 0;
list->length = 0L;
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -615,7 +612,7 @@ int remove_node_tail_double_linked_list(double_linked_list_t *list) {
/* Relink the tail. */
list->tail = current->previous;
free(current);
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -625,7 +622,7 @@ int remove_node_tail_double_linked_list(double_linked_list_t *list) {
/* If free_function is NULL, will use the stdlib's free(). */
int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*free_function)(const void*)) {
/* Sanity check. */
if (list == NULL || list->length == 0 || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
if (!list || !(list->length) || (list->head && !(list->tail)) || (!(list->head) && list->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
@ -633,9 +630,9 @@ int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*
/* Split cases. */
/* Single node case. */
if (list->length == 1) {
if (list->length == 1L) {
/* Free the data. */
if (free_function == NULL)
if (!free_function)
free(list->tail->data);
else
free_function(list->tail->data);
@ -643,7 +640,7 @@ int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*
free(list->tail);
list->head = NULL;
list->tail = NULL;
list->length = 0;
list->length = 0L;
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -651,7 +648,7 @@ int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*
/* Save the pointer to the node to be deleted. */
current = list->tail;
/* Free the data. */
if (free_function == NULL)
if (!free_function)
free(list->tail->data);
else
free_function(list->tail->data);
@ -659,7 +656,7 @@ int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*
/* Relink the tail. */
list->tail = current->previous;
free(current);
list->length = list->length - 1;
(list->length)--;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
@ -668,18 +665,18 @@ int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int clear_list_double_linked_list(double_linked_list_t **list) {
/* Sanity check. */
if (list == NULL || *list == NULL)
if (!list || !(*list))
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
/* Invalid state. */
if (((*list)->head != NULL && (*list)->tail == NULL) || ((*list)->head == NULL && (*list)->tail != NULL))
if (((*list)->head && !((*list)->tail)) || (!((*list)->head) && (*list)->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Variables. */
double_node_t *current, *next;
/* Special case. */
if ((*list)->length == 0) {
if (!((*list)->length)) {
/* Free the list, and set the pointer. */
free(*list);
*list = NULL;
@ -688,7 +685,7 @@ int clear_list_double_linked_list(double_linked_list_t **list) {
/* General case, free all nodes then the list. */
current = (*list)->head;
while (current != NULL) {
while (current) {
/* Save the next pointer. */
next = current->next;
/* Free the current node. */
@ -708,18 +705,18 @@ int clear_list_double_linked_list(double_linked_list_t **list) {
/* If free_function is NULL, will use the stdlib's free(). */
int clear_list_data_double_linked_list(double_linked_list_t **list, void (*free_function)(const void*)) {
/* Sanity check. */
if (list == NULL || *list == NULL)
if (!list || !(*list))
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
/* Invalid state. */
if (((*list)->head != NULL && (*list)->tail == NULL) || ((*list)->head == NULL && (*list)->tail != NULL))
if (((*list)->head && !((*list)->tail)) || (!((*list)->head) && (*list)->tail))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Variables. */
double_node_t *current, *next;
/* Special case. */
if ((*list)->length == 0) {
if (!((*list)->length)) {
/* Free the list, and set the pointer. */
free(*list);
*list = NULL;
@ -728,11 +725,11 @@ int clear_list_data_double_linked_list(double_linked_list_t **list, void (*free_
/* General case, free all nodes then the list. */
current = (*list)->head;
while (current != NULL) {
while (current) {
/* Save the next pointer. */
next = current->next;
/* Free the current node's data. */
if (free_function == NULL)
if (!free_function)
free(current->data);
else
free_function(current->data);

View File

@ -38,7 +38,7 @@
/* The linked list's struct. */
typedef struct double_linked_list {
/* Stores the list's length. */
int length;
size_t length;
/* Stores the head node pointer. */
double_node_t *head;
/* Stores the tail node pointer. */
@ -56,19 +56,19 @@ int append_node_data_double_linked_list(double_linked_list_t *list, void *data);
/* This function creates and prepends a node to the given linked list. */
int prepend_node_data_double_linked_list(double_linked_list_t *list, void *data);
/* This function inserts a node at the ith place. */
int add_node_i_double_linked_list(double_linked_list_t *list, double_node_t *node, int i);
int add_node_i_double_linked_list(double_linked_list_t *list, double_node_t *node, size_t i);
/* This function creates and inserts a node at the ith place. */
int add_node_data_i_double_linked_list(double_linked_list_t *list, void *data, int i);
int add_node_data_i_double_linked_list(double_linked_list_t *list, void *data, size_t i);
/* This function reads the data at the ith node. */
void* read_node_i_double_linked_list(double_linked_list_t *list, int i, int *error);
void* read_node_i_double_linked_list(double_linked_list_t *list, size_t i, int *error);
/* This function retrieves a pointer to the ith node. */
double_node_t* get_node_i_double_linked_list(double_linked_list_t *list, int i, int *error);
double_node_t* get_node_i_double_linked_list(double_linked_list_t *list, size_t i, int *error);
/* This function attempts to find the index of a given node, by pointer. */
int get_node_index_pointer_double_linked_list(double_linked_list_t *list, double_node_t *node);
size_t get_node_index_pointer_double_linked_list(double_linked_list_t *list, double_node_t *node);
/* This function deletes the ith node. */
int remove_node_i_double_linked_list(double_linked_list_t *list, int i);
int remove_node_i_double_linked_list(double_linked_list_t *list, size_t i);
/* This function deletes the ith node and the data stored within. */
int remove_node_i_data_double_linked_list(double_linked_list_t *list, int i, void (*free_function)(const void*));
int remove_node_i_data_double_linked_list(double_linked_list_t *list, size_t i, void (*free_function)(const void*));
/* This function reads the data at the first node. */
void* read_node_head_double_linked_list(double_linked_list_t *list);
/* This function retrieves the first node. */