diff --git a/linked_lists/single_linked_list.c b/linked_lists/single_linked_list.c index 4850c3e..4a7bdf8 100644 --- a/linked_lists/single_linked_list.c +++ b/linked_lists/single_linked_list.c @@ -23,8 +23,8 @@ #include "single_linked_list.h" #endif /* SINGLE_LINKED_LIST_C */ -/* This function initializes a new linked list, returns NULL on failure - data_size is used for storing stored object size. */ -linked_list_t* initialize_single_linked_list(size_t data_size) { +/* This function initializes a new linked list, returns NULL on failure. */ +linked_list_t* initialize_single_linked_list() { /* Variables. */ linked_list_t *result; @@ -38,7 +38,6 @@ linked_list_t* initialize_single_linked_list(size_t data_size) { /* Initialize the internal fields. */ result->length = 0; result->head = NULL; - result->data_size = data_size; /* Return the result. */ return result; @@ -421,7 +420,7 @@ int clear_list_single_linked_list(linked_list_t **list) { /* If free_function is NULL will use STDLIB free instead. */ int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(void*)) { /* Sanity check. */ - if (list == NULL || *list == NULL) + if (!list || !(*list)) return FAILURE_CODE_SIGNLE_LINKED_LIST_C; /* Local variables. */ node_t *current = (*list)->head; @@ -429,10 +428,10 @@ int clear_list_data_single_linked_list(linked_list_t **list, void (*free_functio int length; int count = 0; /* Loop and free. */ - while(current != NULL) { + while(current) { count++; tmp = current->next; - if (free_function != NULL) { + if (free_function) { free_function(current->data); } else { free(current->data); @@ -451,41 +450,21 @@ int clear_list_data_single_linked_list(linked_list_t **list, void (*free_functio /* This function attetmps to serialize a linked list into an array. */ /* The function will allocate and return a pointer to said array on success, NULL on failure. */ -/* The length of the array is sent through the length pointer, and set to 0 on failure. */ -static inline void *serialize_linked_list(linked_list_t *list, size_t *length) { +/* 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. */ +static inline void *serialize_linked_list(linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array) { /* Local variables. */ - void *array; - char *buffer; node_t *current; - int i; + size_t i = 0L; /* Sanity check. */ - if (list == NULL || list->head == NULL || length == NULL || list->length <= 0) + if (!list || !(list->head) || list->length <= 0 || !array || !array_setting_function) return NULL; - /* Set the length. */ - *length = list->length; - /* Attempt to allocate the array. */ - array = (void*) malloc((*length) * (list->data_size)); - /* Check for allocation failure. */ - if (array == NULL) { - *length = 0; - return NULL; - } - /* Attempt to allocate the buffer. */ - buffer = (char*) malloc(list->data_size); - if (buffer == NULL) { - *length = 0; - return NULL; - } - /* Fill the array. */ - current = list->head; - for (i = 0; i < *length; i++) { - /* Attempt to copy to the current buffer. */ - memcpy(buffer, (char*)current->data, list->data_size); - /* Copy the data for the current item. */ - /*memcpy(array + (i * list->data_size), current->data, list->data_size);*/ + for (current = list->head; current; i++) { + /* Set the ith value. */ + array_setting_function(array, i, current->data); /* Advance the pointer. */ current = current->next; } diff --git a/linked_lists/single_linked_list.h b/linked_lists/single_linked_list.h index cc0c39e..b69c73e 100644 --- a/linked_lists/single_linked_list.h +++ b/linked_lists/single_linked_list.h @@ -42,12 +42,10 @@ typedef struct linked_list { int length; /* Stores the head node pointer. */ node_t *head; - /* The size of each of the data values. */ - size_t data_size; } linked_list_t; /* This function initializes a new linked list. */ -linked_list_t* initialize_single_linked_list(size_t data_size); +linked_list_t* initialize_single_linked_list(); /* This function appends a node to the given linked list. */ int append_node_single_linked_list(linked_list_t *list, node_t *node); /* This function prepends a node to the given linked list. */ @@ -92,6 +90,6 @@ int remove_node_tail_data_single_linked_list(linked_list_t *list, void (*free_fu 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. */ int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(void*)); -/* This function attempts to serialize a linked list into an array. */ -static inline void *serialize_linked_list(linked_list_t *list, size_t *length); +/* 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 */ diff --git a/tests/single_linked_list_tests.c b/tests/single_linked_list_tests.c index 1a69c37..019cd56 100644 --- a/tests/single_linked_list_tests.c +++ b/tests/single_linked_list_tests.c @@ -17,6 +17,7 @@ /* Define functions. */ node_t* generate_random_node(); linked_list_t* generate_long_random_list(int length); +void set_array_value_index(void *array, size_t index, void *value); int list_count_length(linked_list_t *list); int test_serialization(); int test_linked_lists(); @@ -54,6 +55,15 @@ node_t* generate_random_node() { return result; } +/* This function is used to set a value at an index in the array. */ +void set_array_value_index(void *array, size_t index, void *value) { + /* Cast. */ + int *arr = (int*)array; + int *val = (int*)value; + /* Set the value. */ + arr[index] = *val; +} + /* This function generates a long list with random values in the nodes, returns NULL on failures. */ /* Length denotes the length of the list. */ linked_list_t* generate_long_random_list(int length) { @@ -122,14 +132,17 @@ int test_serialization() { int i; size_t length; /* Used to allocate and store a value. */ - int *value; - /* Used to store the array. */ - void *array; + int *value, *array; /* Allocations. */ /* Initialize the list. */ linked_list_t *list = initialize_single_linked_list(sizeof(int)); - if (list == NULL) { + if (list == NULL) + return 1; + /* Allocate the array. */ + array = (int*) malloc(sizeof(int) * SERIALIZATION_TEST_LIST_LENGTH); + if (array == NULL) { + free(list); return 1; } @@ -151,10 +164,8 @@ int test_serialization() { append_node_data_single_linked_list(list, value); } - /* Serialize the list. */ - array = serialize_linked_list(list, &length); - /* Check if the serialization failed. */ - if (length == -1 || length != list->length) { + /* Serialize the list and check if the serialization failed. */ + if (!serialize_linked_list(list, set_array_value_index, array)) { /* Clear the list. */ clear_list_data_single_linked_list(&list, NULL); /* Free the array. */ @@ -165,9 +176,7 @@ int test_serialization() { /* Check the values. */ for (i = 0; i < SERIALIZATION_TEST_LIST_LENGTH; i++) { - /* Get the value. */ - *value = *((int *)(array + (i * sizeof(int)))); - if (*value != i) { + if (array[i] != i) { /* Clear the list. */ clear_list_data_single_linked_list(&list, NULL); /* Free the array. */