1
0
Fork 0
C_lib/linked_lists/double_linked_list.h

102 lines
5.6 KiB
C

/*
* C_lib Copyright (C) 2021 Wael Karram.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* This header defines a double-linked-list and related functions. */
/* Use the double nodes. */
#include <stdlib.h> /* Used in allocations and deallocations. */
#ifndef DOUBLE_LINKED_LIST_H
#define DOUBLE_LINKED_LIST_H
#include "../nodes/double_node.h"
/* Include assert.h to allow code generation, but only run with debug flag. */
#ifdef DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif /* NDEBUG. */
#endif /* DEBUG. */
#include <assert.h>
/* Constants. */
/* Function errror/return code on success. */
#define SUCCESS_CODE_DOUBLE_LINKED_LIST_C 1
/* Function errror/return code on failure. */
#define FAILURE_CODE_DOUBLE_LINKED_LIST_C 0
/* Function errror/return code on failure to find index. */
#define INDEX_NOT_FOUND_CODE_DOUBLE_LINKED_LIST_C -1
/* Function errror/return code on invalid input. */
#define INVALID_INPUT_CODE_DOUBLE_LINKED_LIST_C -2
/* Error code returned when input fails sanity check. */
#define CODE_FREE_SUCCESS_MALFORMED_DOUBLE_LINKED_LIST_C -1
/* The default index of the list's head. */
#define INDEX_HEAD_DOUBLE_LINKED_LIST_C 0
/* The linked list's struct. */
typedef struct double_linked_list {
/* Stores the list's length. */
size_t length;
/* Stores the head node pointer. */
double_node_t *head;
/* Stores the tail node pointer. */
double_node_t *tail;
} double_linked_list_t;
/* This function initializes a new linked list. */
static inline double_linked_list_t* initialize_double_linked_list();
/* This function appends a node to the given linked list. */
static inline void append_node_double_linked_list(double_linked_list_t *list, double_node_t *node);
/* This function prepends a node the the given linked list. */
static inline void 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. */
static inline 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. */
static inline int prepend_node_data_double_linked_list(double_linked_list_t *list, void *data);
/* This function inserts a node at the ith place. */
static inline void 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. */
static inline 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. */
static inline void* read_node_i_double_linked_list(double_linked_list_t *list, size_t i);
/* This function retrieves a pointer to the ith node. */
static inline double_node_t* get_node_i_double_linked_list(double_linked_list_t *list, size_t i);
/* This function attempts to find the index of a given node, by pointer. */
static inline size_t get_node_index_pointer_double_linked_list(double_linked_list_t *list, double_node_t *node);
/* This function deletes the ith node. */
static inline void 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. */
static inline void 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. */
static inline void* read_node_head_double_linked_list(double_linked_list_t *list);
/* This function retrieves the first node. */
static inline double_node_t* get_node_head_double_linked_list(double_linked_list_t *list);
/* This function deletes the first node. */
static inline void remove_node_head_double_linked_list(double_linked_list_t *list);
/* This function deletes the first node and the data thereof. */
static inline void remove_node_head_data_double_linked_list(double_linked_list_t *list, void (*free_function)(const void*));
/* This function reads the data at the last node. */
static inline void* read_node_tail_double_linked_list(double_linked_list_t *list);
/* This function retrieves the last node. */
static inline double_node_t* get_node_tail_double_linked_list(double_linked_list_t *list);
/* This function deletes the last node. */
static inline void remove_node_tail_double_linked_list(double_linked_list_t *list);
/* This function deletes the last node and the data thereof. */
static inline void remove_node_tail_data_double_linked_list(double_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 void 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. */
static inline void clear_list_data_double_linked_list(double_linked_list_t **list, void (*free_function)(const void*));
/* This function serializes the list, it relies on an array setting function and an allocated array/buffer. */
static inline void *serialize_double_linked_list(double_linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array);
#endif /* DOUBLE_LINKED_LIST_H */