1
0
Fork 0

Rearchitectured and fixed single linked lists (serialization), next commits shall fix the same issue in other structures.

This commit is contained in:
wael 2022-01-03 16:33:56 +02:00
parent d660572fe2
commit 14a4162fc3
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
3 changed files with 36 additions and 50 deletions

View File

@ -23,8 +23,8 @@
#include "single_linked_list.h" #include "single_linked_list.h"
#endif /* SINGLE_LINKED_LIST_C */ #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. */ /* This function initializes a new linked list, returns NULL on failure. */
linked_list_t* initialize_single_linked_list(size_t data_size) { linked_list_t* initialize_single_linked_list() {
/* Variables. */ /* Variables. */
linked_list_t *result; linked_list_t *result;
@ -38,7 +38,6 @@ linked_list_t* initialize_single_linked_list(size_t data_size) {
/* Initialize the internal fields. */ /* Initialize the internal fields. */
result->length = 0; result->length = 0;
result->head = NULL; result->head = NULL;
result->data_size = data_size;
/* Return the result. */ /* Return the result. */
return 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. */ /* 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*)) { int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(void*)) {
/* Sanity check. */ /* Sanity check. */
if (list == NULL || *list == NULL) if (!list || !(*list))
return FAILURE_CODE_SIGNLE_LINKED_LIST_C; return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
/* Local variables. */ /* Local variables. */
node_t *current = (*list)->head; 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 length;
int count = 0; int count = 0;
/* Loop and free. */ /* Loop and free. */
while(current != NULL) { while(current) {
count++; count++;
tmp = current->next; tmp = current->next;
if (free_function != NULL) { if (free_function) {
free_function(current->data); free_function(current->data);
} else { } else {
free(current->data); 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. */ /* 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 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. */ /* Note that the array has to be allocated by the caller. */
static inline void *serialize_linked_list(linked_list_t *list, size_t *length) { /* 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. */ /* Local variables. */
void *array;
char *buffer;
node_t *current; node_t *current;
int i; size_t i = 0L;
/* Sanity check. */ /* 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; 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. */ /* Fill the array. */
current = list->head; for (current = list->head; current; i++) {
for (i = 0; i < *length; i++) { /* Set the ith value. */
/* Attempt to copy to the current buffer. */ array_setting_function(array, i, current->data);
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);*/
/* Advance the pointer. */ /* Advance the pointer. */
current = current->next; current = current->next;
} }

View File

@ -42,12 +42,10 @@ typedef struct linked_list {
int length; int length;
/* Stores the head node pointer. */ /* Stores the head node pointer. */
node_t *head; node_t *head;
/* The size of each of the data values. */
size_t data_size;
} linked_list_t; } linked_list_t;
/* This function initializes a new linked list. */ /* 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. */ /* This function appends a node to the given linked list. */
int append_node_single_linked_list(linked_list_t *list, node_t *node); int append_node_single_linked_list(linked_list_t *list, node_t *node);
/* This function prepends a node to the given linked list. */ /* 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); 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. */ /* 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*)); 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. */ /* 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, size_t *length); 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 */ #endif /* SINGLE_LINKED_LIST_H */

View File

@ -17,6 +17,7 @@
/* Define functions. */ /* Define functions. */
node_t* generate_random_node(); node_t* generate_random_node();
linked_list_t* generate_long_random_list(int length); 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 list_count_length(linked_list_t *list);
int test_serialization(); int test_serialization();
int test_linked_lists(); int test_linked_lists();
@ -54,6 +55,15 @@ node_t* generate_random_node() {
return result; 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. */ /* This function generates a long list with random values in the nodes, returns NULL on failures. */
/* Length denotes the length of the list. */ /* Length denotes the length of the list. */
linked_list_t* generate_long_random_list(int length) { linked_list_t* generate_long_random_list(int length) {
@ -122,14 +132,17 @@ int test_serialization() {
int i; int i;
size_t length; size_t length;
/* Used to allocate and store a value. */ /* Used to allocate and store a value. */
int *value; int *value, *array;
/* Used to store the array. */
void *array;
/* Allocations. */ /* Allocations. */
/* Initialize the list. */ /* Initialize the list. */
linked_list_t *list = initialize_single_linked_list(sizeof(int)); 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; return 1;
} }
@ -151,10 +164,8 @@ int test_serialization() {
append_node_data_single_linked_list(list, value); append_node_data_single_linked_list(list, value);
} }
/* Serialize the list. */ /* Serialize the list and check if the serialization failed. */
array = serialize_linked_list(list, &length); if (!serialize_linked_list(list, set_array_value_index, array)) {
/* Check if the serialization failed. */
if (length == -1 || length != list->length) {
/* Clear the list. */ /* Clear the list. */
clear_list_data_single_linked_list(&list, NULL); clear_list_data_single_linked_list(&list, NULL);
/* Free the array. */ /* Free the array. */
@ -165,9 +176,7 @@ int test_serialization() {
/* Check the values. */ /* Check the values. */
for (i = 0; i < SERIALIZATION_TEST_LIST_LENGTH; i++) { for (i = 0; i < SERIALIZATION_TEST_LIST_LENGTH; i++) {
/* Get the value. */ if (array[i] != i) {
*value = *((int *)(array + (i * sizeof(int))));
if (*value != i) {
/* Clear the list. */ /* Clear the list. */
clear_list_data_single_linked_list(&list, NULL); clear_list_data_single_linked_list(&list, NULL);
/* Free the array. */ /* Free the array. */