96 lines
5.0 KiB
C
96 lines
5.0 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"
|
|
|
|
/* 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. */
|
|
int 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. */
|
|
double_linked_list_t* initialize_double_linked_list();
|
|
/* This function appends a node to the given linked list. */
|
|
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(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(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);
|
|
/* 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);
|
|
/* 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);
|
|
/* 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);
|
|
/* 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);
|
|
/* This function deletes the ith node. */
|
|
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(double_linked_list_t *list, int 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. */
|
|
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(double_linked_list_t *list);
|
|
/* This function deletes the first node and the data thereof. */
|
|
int 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. */
|
|
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(double_linked_list_t *list);
|
|
/* This function deletes the last node. */
|
|
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(double_linked_list_t *list, void (*free_function)(const void*));
|
|
/* This function deletes all the nodes of the given list and the list itself. */
|
|
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(double_linked_list_t **list, void (*free_function)(const void*));
|
|
/* This function serializes the list. */
|
|
void **serialize_double_linked_list(double_linked_list_t *list, int *length);
|
|
|
|
#endif /* SINGLE_LINKED_LIST_H */
|