1
0
Fork 0

Done implementing double-linked-lists, and rewrote single linked lists to play along nicely (different function names)

This commit is contained in:
wael 2021-09-04 17:18:06 +03:00
parent d12c3df291
commit c6b3367d27
9 changed files with 192 additions and 144 deletions

View File

@ -8,13 +8,19 @@ Wael Karram
.AB no
Documentation for single_node.h
.AE
.ND
.PP
This header defines a single node, with the prepoccessor variable SINGLE_NODE_H used as an include guard.
.br
Defines a single struct
.UL node_t which has the fields
.I "next"
and
.I "data"
, the former of which is another node pointer, and the latter is a void pointer.
.UL node_t \0which
has the fields
.I "next" \0and
.I "data" ,
the former of which is another node pointer, and the latter is a void pointer.
.br
It is of note that this header type-defines the aformentioned node as
.I "node_t" .

Binary file not shown.

View File

@ -9,7 +9,7 @@
/* Function implementations. */
/* This function initializes a new double-linked list, returns NULL on failure. */
double_linked_list_t* initialize_linked_list() {
double_linked_list_t* initialize_double_linked_list() {
/* Local variables. */
double_linked_list_t* list;
@ -31,7 +31,7 @@ double_linked_list_t* initialize_linked_list() {
/* This function appends a node to the given 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_t *list, double_node_t *node){
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))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -55,7 +55,7 @@ int append_node(double_linked_list_t *list, double_node_t *node){
/* This function prepends a node the the given linked list. */
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int prepend_node(double_linked_list_t *list, double_node_t *node){
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))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -79,7 +79,7 @@ int prepend_node(double_linked_list_t *list, double_node_t *node){
/* This function creates and appends a node to the given linked list. */
/* 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_t *list, void *data){
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))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -94,12 +94,12 @@ int append_node_data(double_linked_list_t *list, void *data){
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Delegate. */
return append_node(list, node);
return append_node_double_linked_list(list, node);
}
/* This function creates and prepends a node to the given linked list. */
/* 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_t *list, void *data){
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))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -114,21 +114,21 @@ int prepend_node_data(double_linked_list_t *list, void *data){
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Delegate. */
return prepend_node(list, node);
return prepend_node_double_linked_list(list, node);
}
/* 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_t *list, double_node_t *node, int i) {
int add_node_i_double_linked_list(double_linked_list_t *list, double_node_t *node, int 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)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Deal with special "easy" cases. */
if (i == 0)
return prepend_node(list, node);
return prepend_node_double_linked_list(list, node);
if (i == list->length)
return append_node(list, node);
return append_node_double_linked_list(list, node);
/* Local variables. */
int index;
@ -153,7 +153,7 @@ int add_node_i(double_linked_list_t *list, double_node_t *node, int i) {
/* 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_t *list, void *data, int i) {
int add_node_data_i_double_linked_list(double_linked_list_t *list, void *data, int 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)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -168,13 +168,13 @@ int add_node_data_i(double_linked_list_t *list, void *data, int i) {
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
/* Delegate. */
return add_node_i(list, node, i);
return add_node_i_double_linked_list(list, node, 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_t *list, int i, int *error) {
void* read_node_i_double_linked_list(double_linked_list_t *list, int i, int *error) {
/* 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) {
*error = FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -200,7 +200,7 @@ void* read_node_i(double_linked_list_t *list, int i, int *error) {
/* 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_t *list, int i, int *error) {
double_node_t* get_node_i_double_linked_list(double_linked_list_t *list, int i, int *error) {
/* 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) {
*error = FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -224,11 +224,11 @@ double_node_t* get_node_i(double_linked_list_t *list, int i, int *error) {
}
/* This function attempts to find the index of a given node, by pointer. */
/* Returns INDEX_NOT_FOUND_CODE_LINKED_LIST_C on failure. */
int get_node_index_pointer(double_linked_list_t *list, double_node_t *node) {
/* 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) {
/* Sanity check. */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL) || node == NULL)
return INDEX_NOT_FOUND_CODE_LINKED_LIST_C;
return INDEX_NOT_FOUND_CODE_DOUBLE_LINKED_LIST_C;
/* Local variables. */
@ -242,12 +242,12 @@ int get_node_index_pointer(double_linked_list_t *list, double_node_t *node) {
}
/* Return according to if found or not. */
return (current != NULL) ? index : INDEX_NOT_FOUND_CODE_LINKED_LIST_C;
return (current != NULL) ? index : INDEX_NOT_FOUND_CODE_DOUBLE_LINKED_LIST_C;
}
/* 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_t *list, int i) {
int remove_node_i_double_linked_list(double_linked_list_t *list, int 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)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -256,6 +256,24 @@ int remove_node_i(double_linked_list_t *list, int i) {
double_node_t* current;
int index;
/* Special case of an empty list. */
if (list->length == 0) {
/* Done nothing. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
/* Special case of a list with a single node. */
if (list->length == 1) {
/* Clear the node. */
free(list->head);
/* "Relink" */
list->head = NULL;
list->tail = NULL;
list->length = 0;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
/* Loop till we get to the desired node. */
while (index < i) {
/* Increment. */
@ -268,7 +286,7 @@ int remove_node_i(double_linked_list_t *list, int i) {
if (current->previous == NULL) {
/* Update head. */
list->head = current->next;
current->next->previous = NULL;
list->head->previous = NULL;
/* Free node struct. */
free(current);
current = NULL;
@ -306,7 +324,7 @@ int remove_node_i(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_t *list, int i, void (*free_function)()) {
int remove_node_i_data_double_linked_list(double_linked_list_t *list, int i, void (*free_function)()) {
/* 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)
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -315,6 +333,29 @@ int remove_node_i_data(double_linked_list_t *list, int i, void (*free_function)(
double_node_t* current;
int index;
/* Special case of an empty list. */
if (list->length == 0) {
/* Done nothing. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
/* Special case of a list with a single node. */
if (list->length == 1) {
/* Free the data within the node. */
if (free_function == NULL)
free(list->head->data);
else
free_function(list->head->data);
/* Clear the node. */
free(list->head);
/* "Relink" */
list->head = NULL;
list->tail = NULL;
list->length = 0;
/* Done. */
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
}
/* Loop till we get to the desired node. */
while (index < i) {
/* Increment. */
@ -376,7 +417,7 @@ int remove_node_i_data(double_linked_list_t *list, int i, void (*free_function)(
/* This function reads the data at the first node. */
/* Returns NULL on error or empty list. */
void* read_node_head(double_linked_list_t *list) {
void* read_node_head_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0 || list->head == NULL)
return NULL;
@ -387,7 +428,7 @@ void* read_node_head(double_linked_list_t *list) {
/* This function retrieves the first node. */
/* Returns NULL on error or empty list. */
double_node_t* get_node_head(double_linked_list_t *list) {
double_node_t* get_node_head_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0)
return NULL;
@ -398,7 +439,7 @@ double_node_t* get_node_head(double_linked_list_t *list) {
/* This function deletes the first node. */
/* 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_t *list) {
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))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -440,7 +481,7 @@ int remove_node_head(double_linked_list_t *list) {
/* This function deletes the first node and the data thereof. */
/* 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_head_data(double_linked_list_t *list, void (*free_function)()) {
int remove_node_head_data_double_linked_list(double_linked_list_t *list, void (*free_function)()) {
/* Sanity check. */
if (list == NULL || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -489,7 +530,7 @@ int remove_node_head_data(double_linked_list_t *list, void (*free_function)()) {
/* This function reads the data at the last node. */
/* Returns NULL on error or empty list. */
void* read_node_tail(double_linked_list_t *list) {
void* read_node_tail_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0 || list->tail == NULL)
return NULL;
@ -500,7 +541,7 @@ void* read_node_tail(double_linked_list_t *list) {
/* This function retrieves the last node. */
/* Returns NULL on error or empty list. */
double_node_t* get_node_tail(double_linked_list_t *list) {
double_node_t* get_node_tail_double_linked_list(double_linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->length == 0)
return NULL;
@ -512,7 +553,7 @@ double_node_t* get_node_tail(double_linked_list_t *list) {
/* This function deletes the last node. */
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
/* Failure is also when the list state isn't properly defined! */
int remove_node_tail(double_linked_list_t *list) {
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))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -544,7 +585,7 @@ int remove_node_tail(double_linked_list_t *list) {
/* This function deletes the last node and the data thereof. */
/* 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_tail_data(double_linked_list_t *list, void (*free_function)()) {
int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*free_function)()) {
/* Sanity check. */
if (list == NULL || list->length == 0 || (list->head != NULL && list->tail == NULL) || (list->head == NULL && list->tail != NULL))
return FAILURE_CODE_DOUBLE_LINKED_LIST_C;
@ -587,7 +628,7 @@ int remove_node_tail_data(double_linked_list_t *list, void (*free_function)()) {
/* This function deletes all the nodes of the given list and the list itself. */
/* Returns SUCCESS_CODE_DOUBLE_LINKED_LIST_C on success, and FAILURE_CODE_DOUBLE_LINKED_LIST_C on failure. */
int clear_list(double_linked_list_t **list) {
int clear_list_double_linked_list(double_linked_list_t **list) {
/* Sanity check. */
if (list == NULL || *list == NULL)
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;
@ -627,7 +668,7 @@ int clear_list(double_linked_list_t **list) {
/* This function deletes all the nodes and data stored within of the given list and the list itself. */
/* 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 clear_list_data(double_linked_list_t **list, void (*free_function)()) {
int clear_list_data_double_linked_list(double_linked_list_t **list, void (*free_function)()) {
/* Sanity check. */
if (list == NULL || *list == NULL)
return SUCCESS_CODE_DOUBLE_LINKED_LIST_C;

View File

@ -6,12 +6,12 @@
#include "../nodes/double_node.h"
/* Constants. */
#define SUCCESS_CODE_DOUBLE_LINKED_LIST_C 1
#define FAILURE_CODE_DOUBLE_LINKED_LIST_C 0
#define INDEX_NOT_FOUND_CODE_LINKED_LIST_C -1
#define INVALID_INPUT_CODE_LINKED_LIST_C -2
#define CODE_FREE_SUCCESS_MALFORMED_LINKED_LIST_C -1
#define INDEX_HEAD_DOUBLE_LINKED_LIST_C 0
#define SUCCESS_CODE_DOUBLE_LINKED_LIST_C 1
#define FAILURE_CODE_DOUBLE_LINKED_LIST_C 0
#define INDEX_NOT_FOUND_CODE_DOUBLE_LINKED_LIST_C -1
#define INVALID_INPUT_CODE_DOUBLE_LINKED_LIST_C -2
#define CODE_FREE_SUCCESS_MALFORMED_DOUBLE_LINKED_LIST_C -1
#define INDEX_HEAD_DOUBLE_LINKED_LIST_C 0
/* The actual linked list struct. */
typedef struct double_linked_list {
@ -24,48 +24,48 @@ typedef struct double_linked_list {
} double_linked_list_t;
/* This function initializes a new linked list. */
double_linked_list_t* initialize_linked_list();
double_linked_list_t* initialize_double_linked_list();
/* This function appends a node to the given linked list. */
int append_node(double_linked_list_t *list, double_node_t *node);
int append_node_double_linked_list(double_linked_list_t *list, double_node_t *node);
/* This function prepends a node the the given linked list. */
int prepend_node(double_linked_list_t *list, double_node_t *node);
int prepend_node_double_linked_list(double_linked_list_t *list, double_node_t *node);
/* This function creates and appends a node to the given linked list. */
int append_node_data(double_linked_list_t *list, void *data);
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_t *list, void *data);
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_t *list, double_node_t *node, int i);
int add_node_i_double_linked_list(double_linked_list_t *list, double_node_t *node, int i);
/* This function creates and inserts a node at the ith place. */
int add_node_data_i(double_linked_list_t *list, void *data, int i);
int add_node_data_i_double_linked_list(double_linked_list_t *list, void *data, int i);
/* This function reads the data at the ith node. */
void* read_node_i(double_linked_list_t *list, int i, int *error);
void* read_node_i_double_linked_list(double_linked_list_t *list, int i, int *error);
/* This function retrieves a pointer to the ith node. */
double_node_t* get_node_i(double_linked_list_t *list, int i, int *error);
double_node_t* get_node_i_double_linked_list(double_linked_list_t *list, int i, int *error);
/* This function attempts to find the index of a given node, by pointer. */
int get_node_index_pointer(double_linked_list_t *list, double_node_t *node);
int 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_t *list, int i);
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. */
int remove_node_i_data(double_linked_list_t *list, int i, void (*free_function)());
int remove_node_i_data_double_linked_list(double_linked_list_t *list, int i, void (*free_function)());
/* This function reads the data at the first node. */
void* read_node_head(double_linked_list_t *list);
void* read_node_head_double_linked_list(double_linked_list_t *list);
/* This function retrieves the first node. */
double_node_t* get_node_head(double_linked_list_t *list);
double_node_t* get_node_head_double_linked_list(double_linked_list_t *list);
/* This function deletes the first node. */
int remove_node_head(double_linked_list_t *list);
int remove_node_head_double_linked_list(double_linked_list_t *list);
/* This function deletes the first node and the data thereof. */
int remove_node_head_data(double_linked_list_t *list, void (*free_function)());
int remove_node_head_data_double_linked_list(double_linked_list_t *list, void (*free_function)());
/* This function reads the data at the last node. */
void* read_node_tail(double_linked_list_t *list);
void* read_node_tail_double_linked_list(double_linked_list_t *list);
/* This function retrieves the last node. */
double_node_t* get_node_tail(double_linked_list_t *list);
double_node_t* get_node_tail_double_linked_list(double_linked_list_t *list);
/* This function deletes the last node. */
int remove_node_tail(double_linked_list_t *list);
int remove_node_tail_double_linked_list(double_linked_list_t *list);
/* This function deletes the last node and the data thereof. */
int remove_node_tail_data(double_linked_list_t *list, void (*free_function)());
int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*free_function)());
/* This function deletes all the nodes of the given list and the list itself. */
int clear_list(double_linked_list_t **list);
int clear_list_double_linked_list(double_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(double_linked_list_t **list, void (*free_function)());
int clear_list_data_double_linked_list(double_linked_list_t **list, void (*free_function)());
#endif /* SINGLE_LINKED_LIST_H */

View File

@ -6,7 +6,7 @@
#endif /* SINGLE_LINKED_LIST_C */
/* This function initializes a new linked list, returns NULL on failure. */
linked_list_t* initialize_linked_list() {
linked_list_t* initialize_single_linked_list() {
/* Variables. */
linked_list_t *result;
@ -26,7 +26,7 @@ linked_list_t* initialize_linked_list() {
}
/* This function appends a node to the given linked list, returns 1 on success and 0 on failure. */
int append_node(linked_list_t *list, node_t *node) {
int append_node_single_linked_list(linked_list_t *list, node_t *node) {
/* Input sanity check. */
if (list == NULL || node == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -48,7 +48,7 @@ int append_node(linked_list_t *list, node_t *node) {
}
/* This function prepends a node to the given linked list, returns 1 on success and 0 on failure. */
int prepend_node(linked_list_t *list, node_t *node) {
int prepend_node_single_linked_list(linked_list_t *list, node_t *node) {
/* Input sanity check. */
if (list == NULL || node == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -66,7 +66,7 @@ int prepend_node(linked_list_t *list, node_t *node) {
}
/* This function creates and appends a node to the given linked list, returns 1 on success and 0 on failure. */
int append_node_data(linked_list_t *list, void *data) {
int append_node_data_single_linked_list(linked_list_t *list, void *data) {
/* Input sanity check. */
if (list == NULL || data == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -77,11 +77,11 @@ int append_node_data(linked_list_t *list, void *data) {
/* Insert the data. */
node->data = data;
/* Delegate the work. */
return append_node(list, node);
return append_node_single_linked_list(list, node);
}
/* This function creates and prepends a node to the given linked list, returns 1 on success and 0 on failure. */
int prepend_node_data(linked_list_t *list, void *data) {
int prepend_node_data_single_linked_list(linked_list_t *list, void *data) {
/* Input sanity check. */
if (list == NULL || data == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -92,11 +92,11 @@ int prepend_node_data(linked_list_t *list, void *data) {
/* Insert the data. */
node->data = data;
/* Delegate the work. */
return prepend_node(list, node);
return prepend_node_single_linked_list(list, node);
}
/* This function inserts a node at the ith place, returns 1 on success and 0 on failure. */
int add_node_i(linked_list_t *list, node_t *node, int i) {
int add_node_i_single_linked_list(linked_list_t *list, node_t *node, int i) {
/* Input sanity check. */
if (list == NULL || node == NULL || list->length <= i)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -120,7 +120,7 @@ int add_node_i(linked_list_t *list, node_t *node, int i) {
}
/* This function creates and inserts a node at the ith place, returns 1 in success and 0 on failure. */
int add_node_data_i(linked_list_t *list, void *data, int i) {
int add_node_data_i_single_linked_list(linked_list_t *list, void *data, int i) {
/* Input sanity check. */
if (list == NULL || list->length <= i)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -131,11 +131,11 @@ int add_node_data_i(linked_list_t *list, void *data, int i) {
/* Insert the data. */
node->data = data;
/* Delegate. */
return add_node_i(list, node, i);
return add_node_i_single_linked_list(list, node, i);
}
/* This function reads the data at the ith node, returns NULL on failure or NULL data! */
void* read_node_i(linked_list_t *list, int i) {
void* read_node_i_single_linked_list(linked_list_t *list, int i) {
/* Input sanity check. */
if (list == NULL || list->length <= i)
return NULL;
@ -148,7 +148,7 @@ void* read_node_i(linked_list_t *list, int i) {
}
/* This function retrieves a pointer to the ith node, returns NULL on failure. */
node_t* get_node_i(linked_list_t *list, int i) {
node_t* get_node_i_single_linked_list(linked_list_t *list, int i) {
/* Input sanity check */
if (list == NULL || list->length <= i)
return NULL;
@ -162,27 +162,27 @@ node_t* get_node_i(linked_list_t *list, int i) {
/* This function attempts to find the index of a given node, by pointer. */
/* Returns -2 on invalid input, and -1 when no found. */
int get_node_index_pointer(linked_list_t *list, node_t *node){
int get_node_index_pointer_single_linked_list(linked_list_t *list, node_t *node){
/* Sanity check. */
if (list == NULL || node == NULL)
return INVALID_INPUT_CODE_LINKED_LIST_C;
return INVALID_INPUT_CODE_SINGLE_LINKED_LIST_C;
/* Local variables. */
node_t *current;
int i = 0;
/* Attempt to search. */
if (list->head == NULL)
return INDEX_NOT_FOUND_CODE_LINKED_LIST_C;
return INDEX_NOT_FOUND_CODE_SINGLE_LINKED_LIST_C;
current = list->head;
while (current != node) {
current = current->next;
i++;
}
/* Check if we found it or not. */
return (i >= list->length) ? INDEX_NOT_FOUND_CODE_LINKED_LIST_C : i;
return (i >= list->length) ? INDEX_NOT_FOUND_CODE_SINGLE_LINKED_LIST_C : i;
}
/* This function deletes the ith node, returns 1 on success and 0 on failure. */
int remove_node_i(linked_list_t *list, int i) {
int remove_node_i_single_linked_list(linked_list_t *list, int i) {
/* Input sanity check */
if (list == NULL || list->length <= i)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -212,7 +212,7 @@ int remove_node_i(linked_list_t *list, int i) {
/* This function deletes the ith node with the data stored within it, returns 1 on success and 0 on failure. */
/* A free function pointer of NULL value causes the use of the internal stdlib free(). */
int remove_node_i_data(linked_list_t *list, int i, void (*free_function)()) {
int remove_node_i_data_single_linked_list(linked_list_t *list, int i, void (*free_function)()) {
/* Input sanity check */
if (list == NULL || list->length <= i)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -249,71 +249,71 @@ int remove_node_i_data(linked_list_t *list, int i, void (*free_function)()) {
}
/* This function reads the data at the first node, note that it will return NULL on invalid input or first node being NULL. */
void* read_node_head(linked_list_t *list) {
void* read_node_head_single_linked_list(linked_list_t *list) {
/* Delegate. */
return read_node_i(list, INDEX_HEAD_LINKED_LIST_C);
return read_node_i_single_linked_list(list, INDEX_HEAD_LINKED_LIST_C);
}
/* This function retrieves the first node, note that it will return NULL on invalid input or first node being NULL. */
node_t* get_node_head(linked_list_t *list) {
node_t* get_node_head_single_linked_list(linked_list_t *list) {
/* Delegate. */
return get_node_i(list, INDEX_HEAD_LINKED_LIST_C);
return get_node_i_single_linked_list(list, INDEX_HEAD_LINKED_LIST_C);
}
/* This function deletes the first node, returns 1 on success and 0 on failure. */
int remove_node_head(linked_list_t *list) {
int remove_node_head_single_linked_list(linked_list_t *list) {
/* Delegate. */
return remove_node_i(list, INDEX_HEAD_LINKED_LIST_C);
return remove_node_i_single_linked_list(list, INDEX_HEAD_LINKED_LIST_C);
}
/* This function deletes the first node and the contents thereof, returns 1 on success and 0 on failure. */
/* If the free_function is NULL, will use stdlib's free() instead. */
int remove_node_head_data(linked_list_t *list, void (*free_function)()) {
int remove_node_head_data_single_linked_list(linked_list_t *list, void (*free_function)()) {
/* Delegate. */
return remove_node_i_data(list, INDEX_HEAD_LINKED_LIST_C, free_function);
return remove_node_i_data_single_linked_list(list, INDEX_HEAD_LINKED_LIST_C, free_function);
}
/* This function reads the data at the last node, will return NULL on invalid input or first node being NULL. */
void* read_node_tail(linked_list_t *list) {
void* read_node_tail_single_linked_list(linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->head == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
/* Delegate. */
return read_node_i(list, list->length - 1);
return read_node_i_single_linked_list(list, list->length - 1);
}
/* This function retrieves the last node, will return NULL on invalid input or the first node being NULL. */
node_t* get_node_tail(linked_list_t *list) {
node_t* get_node_tail_single_linked_list(linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->head == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
/* Delegate. */
return get_node_i(list, list->length - 1);
return get_node_i_single_linked_list(list, list->length - 1);
}
/* This function deletes the last node, returns 1 on success and 0 on failure. */
int remove_node_tail(linked_list_t *list) {
int remove_node_tail_single_linked_list(linked_list_t *list) {
/* Sanity check. */
if (list == NULL || list->head == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
/* Delegate. */
return remove_node_i(list, list->length - 1);
return remove_node_i_single_linked_list(list, list->length - 1);
}
/* This function deletes the last node and the data thereof, returns 1 on success and 0 on failure. */
/* If the free_function is NULL, will use stdlib's free() instead. */
int remove_node_tail_data(linked_list_t *list, void (*free_function)()) {
int remove_node_tail_data_single_linked_list(linked_list_t *list, void (*free_function)()) {
/* Sanity check. */
if (list == NULL || list->head == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
/* Delegate. */
return remove_node_i_data(list, list->length - 1, free_function);
return remove_node_i_data_single_linked_list(list, list->length - 1, free_function);
}
/* This function deletes all the nodes of the given list and the list itself. */
/* Note that it doesn't touch the data, returns 1 on success and 0 on failure - failure is potentially undefined behavior! */
/* Will return -1 when the list was freed successfully but it was initially malformed. */
int clear_list(linked_list_t **list) {
int clear_list_single_linked_list(linked_list_t **list) {
/* Sanity check. */
if (list == NULL || *list == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -335,13 +335,13 @@ int clear_list(linked_list_t **list) {
/* Should all be freed by now, free the list and return. */
free(*list);
*list = NULL;
return (count == length) ? SUCCESS_CODE_SIGNLE_LINKED_LIST_C : CODE_FREE_SUCCESS_MALFORMED_LINKED_LIST_C;
return (count == length) ? SUCCESS_CODE_SIGNLE_LINKED_LIST_C : CODE_FREE_SUCCESS_MALFORMED_SINGLE_LINKED_LIST_C;
}
/* 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. */
int clear_list_data(linked_list_t **list, void (*free_function)()) {
int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)()) {
/* Sanity check. */
if (list == NULL || *list == NULL)
return FAILURE_CODE_SIGNLE_LINKED_LIST_C;
@ -368,5 +368,5 @@ int clear_list_data(linked_list_t **list, void (*free_function)()) {
/* Should all be freed by now, free the list and return. */
free(*list);
*list = NULL;
return (count == length) ? SUCCESS_CODE_SIGNLE_LINKED_LIST_C : CODE_FREE_SUCCESS_MALFORMED_LINKED_LIST_C;
return (count == length) ? SUCCESS_CODE_SIGNLE_LINKED_LIST_C : CODE_FREE_SUCCESS_MALFORMED_SINGLE_LINKED_LIST_C;
}

View File

@ -6,12 +6,12 @@
#include "../nodes/single_node.h"
/* Constants. */
#define SUCCESS_CODE_SIGNLE_LINKED_LIST_C 1
#define FAILURE_CODE_SIGNLE_LINKED_LIST_C 0
#define INDEX_NOT_FOUND_CODE_LINKED_LIST_C -1
#define INVALID_INPUT_CODE_LINKED_LIST_C -2
#define CODE_FREE_SUCCESS_MALFORMED_LINKED_LIST_C -1
#define INDEX_HEAD_LINKED_LIST_C 0
#define SUCCESS_CODE_SIGNLE_LINKED_LIST_C 1
#define FAILURE_CODE_SIGNLE_LINKED_LIST_C 0
#define INDEX_NOT_FOUND_CODE_SINGLE_LINKED_LIST_C -1
#define INVALID_INPUT_CODE_SINGLE_LINKED_LIST_C -2
#define CODE_FREE_SUCCESS_MALFORMED_SINGLE_LINKED_LIST_C -1
#define INDEX_HEAD_LINKED_LIST_C 0
/* The actual linked list struct. */
typedef struct linked_list {
@ -22,49 +22,49 @@ typedef struct linked_list {
} linked_list_t;
/* This function initializes a new linked list. */
linked_list_t* initialize_linked_list();
linked_list_t* initialize_single_linked_list();
/* This function appends a node to the given linked list. */
int append_node(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. */
int prepend_node(linked_list_t *list, node_t *node);
int prepend_node_single_linked_list(linked_list_t *list, node_t *node);
/* This function creates and appends a node to the given linked list. */
int append_node_data(linked_list_t *list, void *data);
int append_node_data_single_linked_list(linked_list_t *list, void *data);
/* This function creates and prepends a node to the given linked list. */
int prepend_node_data(linked_list_t *list, void *data);
int prepend_node_data_single_linked_list(linked_list_t *list, void *data);
/* This function inserts a node at the ith place. */
int add_node_i(linked_list_t *list, node_t *node, int i);
int add_node_i_single_linked_list(linked_list_t *list, node_t *node, int i);
/* This function creates and inserts a node at the ith place. */
int add_node_data_i(linked_list_t *list, void *data, int i);
int add_node_data_i_single_linked_list(linked_list_t *list, void *data, int i);
/* This function reads the data at the ith node. */
void* read_node_i(linked_list_t *list, int i);
void* read_node_i_single_linked_list(linked_list_t *list, int i);
/* This function retrieves a pointer to the ith node. */
node_t* get_node_i(linked_list_t *list, int i);
node_t* get_node_i_single_linked_list(linked_list_t *list, int i);
/* This function attempts to find the index of a given node, by pointer. */
int get_node_index_pointer(linked_list_t *list, node_t *node);
int get_node_index_pointer_single_linked_list(linked_list_t *list, node_t *node);
/* This function attempts to find the index of a given node, by data. - TODO: implement! */
/* This function deletes the ith node. */
int remove_node_i(linked_list_t *list, int i);
int remove_node_i_single_linked_list(linked_list_t *list, int i);
/* This function deletes the ith node and the data stored within. */
int remove_node_i_data(linked_list_t *list, int i, void (*free_function)());
int remove_node_i_data_single_linked_list(linked_list_t *list, int i, void (*free_function)());
/* This function reads the data at the first node. */
void* read_node_head(linked_list_t *list);
void* read_node_head_single_linked_list(linked_list_t *list);
/* This function retrieves the first node. */
node_t* get_node_head(linked_list_t *list);
node_t* get_node_head_single_linked_list(linked_list_t *list);
/* This function deletes the first node. */
int remove_node_head(linked_list_t *list);
int remove_node_head_single_linked_list(linked_list_t *list);
/* This function deletes the first node and the data thereof. */
int remove_node_head_data(linked_list_t *list, void (*free_function)());
int remove_node_head_data_single_linked_list(linked_list_t *list, void (*free_function)());
/* This function reads the data at the last node. */
void* read_node_tail(linked_list_t *list);
void* read_node_tail_single_linked_list(linked_list_t *list);
/* This function retrieves the last node. */
node_t* get_node_tail(linked_list_t *list);
node_t* get_node_tail_single_linked_list(linked_list_t *list);
/* This function deletes the last node. */
int remove_node_tail(linked_list_t *list);
int remove_node_tail_single_linked_list(linked_list_t *list);
/* This function deletes the last node and the data thereof. */
int remove_node_tail_data(linked_list_t *list, void (*free_function)());
int remove_node_tail_data_single_linked_list(linked_list_t *list, void (*free_function)());
/* This function deletes all the nodes of the given list and the list itself. */
int clear_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. */
int clear_list_data(linked_list_t **list, void (*free_function)());
int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)());
#endif /* SINGLE_LINKED_LIST_H */

View File

@ -1,4 +1,5 @@
Everything is implemented and tested, only doubly-linked lists need both implementation (partial) and testing (nothing done yet).
Everything is implemented and tested.
TODO: imlement better tests, maybe even use a testing framework?
Future goals:
Implement more data structures, such as hash tables, hash maps, more trees.

Binary file not shown.

View File

@ -63,7 +63,7 @@ linked_list_t* generate_long_random_list(int length) {
int i;
/* Allocate the list. */
list = initialize_linked_list();
list = initialize_single_linked_list();
if (list == NULL)
return NULL;
@ -75,7 +75,7 @@ linked_list_t* generate_long_random_list(int length) {
return NULL;
}
/* Attempt to append. */
if (!append_node(list, current)) {
if (!append_node_single_linked_list(list, current)) {
free(current->data);
free(current);
free(list);
@ -85,11 +85,11 @@ linked_list_t* generate_long_random_list(int length) {
for (i = 1; i < length; i++) {
/* Generate and attempt to append. */
current = generate_random_node();
if (!append_node(list, current)) {
if (!append_node_single_linked_list(list, current)) {
/* Abort. */
free(current->data);
free(current);
clear_list_data(&list, NULL);
clear_list_data_single_linked_list(&list, NULL);
return NULL;
}
}
@ -122,7 +122,7 @@ int test_linked_lists() {
node_t *node, *tail;
/* Initialize an empty list. */
list = initialize_linked_list();
list = initialize_single_linked_list();
/* Check that it was correctly created. */
assert(list != NULL);
assert(list->length == INITIAL_LIST_LENGTH);
@ -132,15 +132,15 @@ int test_linked_lists() {
node = generate_random_node();
/* Attempt to add it to the list. */
append_node(list, node);
append_node_single_linked_list(list, node);
/* Check that the list grew and the head got set. */
assert(list->length == LIST_LENGTH_ONE);
assert(list->head != NULL && list->head == node);
assert(get_node_i(list, 0) == node);
assert(get_node_index_pointer(list, node) == 0);
assert(get_node_i_single_linked_list(list, 0) == node);
assert(get_node_index_pointer_single_linked_list(list, node) == 0);
/* Attempt to free the nodes and then the list. */
remove_node_i_data(list, 0, NULL);
remove_node_i_data_single_linked_list(list, 0, NULL);
assert(list->length == INITIAL_LIST_LENGTH);
assert(list->head == NULL);
node = NULL;
@ -156,13 +156,13 @@ int test_linked_lists() {
assert(list->length == LIST_LENGTH_LONG_TEST);
assert(list->length == list_count_length(list));
/* Test the get head/tail functions. */
assert(get_node_head(list) == list->head);
tail = get_node_tail(list);
assert(get_node_head_single_linked_list(list) == list->head);
tail = get_node_tail_single_linked_list(list);
assert(tail != NULL && tail->next == NULL);
/* Test the indexer functions. */
assert(get_node_index_pointer(list, tail) == (list->length - 1));
assert(get_node_index_pointer_single_linked_list(list, tail) == (list->length - 1));
/* Test the freeing functions. */
clear_list_data(&list, NULL);
clear_list_data_single_linked_list(&list, NULL);
assert(list == NULL);
/* Ran successfully, return 0. */