C implementation of a doubly-linked list datatype.
#include "dll.h"
Make sure to edit dll.h to set the values of DBLL_TYPE and DBLL_VOID as per your usage (void* vs int)
To use it as a linked list of void*s, set the following values:
#define DBLL_TYPE void*
#define DBLL_VOID 1
For a linked list of ints, set:
#define DBLL_TYPE int
#define DBLL_VOID 0
The dbll_append() call pulls double duty for appending and creating new lists.
struct DBLL_Node* list = dbll_append(NULL, 0);
dbll_append() returns NULL in case of an allocation failure and the original pointer it was passed otherwise. If NULL was passed in, it will return a new list node.
list = dbll_append(list, 1);
Returns a pointer to the newly prepended node, ie the new first node of the list, or NULL on allocation failure.
list = dbll_prepend(list, 2);
If NULL is passed as the first argument, it returns a new list with the given values. The second argument is the number of values to append. MAKE SURE THERE ARE ATLEAST AS MANY VALUES IN THE ARRAY AS YOU INTEND TO APPEND!
int arr[] = {1, 2, 3, 4};
dbll_append_values(list, arr, 3);
If a valid DBLL_Node pointer is passed in, it appends the specified count of the specified value to the end of that list, and returns a new list if NULL is passed in.
struct DBLL_Node* list2 = dbll_append_blanks(NULL, 0, 3);
// Creates a new list with 3 elements, each with value 0.
Returns zero if NULL is passed in.
printf("Length of created list is %d\n", dbll_get_length(list));
Set the value stored in the node at the given (absolute, zero-indexed) index to the given value. If the index does not exist in the list, returns -1. Returns 0 on success.
dbll_put_at(list2, 2, 69); // sets the third element of list2 to 69
dbll_insert_node() will return 2 on malloc failure, 1 if null was passed in, 0 on success.
int before = 0; // a boolean specifying whether to insert before or after the given element
int elem = 55; // the element to insert
int r = dbll_insert_node(list, elem, before);
Returns NULL if NULL is passed in.
struct DBLL_Node* last = dbll_get_last_node(list);
Returns NULL if NULL is passed in.
struct DBLL_Node* first = dbll_get_first_node(list);
If the nth index does not exist, sets errno to EINVAL and: * Returns -1 in int mode * Returns NULL in void mode*
int val = dbll_value_at(list, 2);
Returns a NULL pointer and sets errno to EINVAL if the nth index does not exist.
struct DBLL_Node* nth = dbll_get_nth_node(list, 2);
Prints out the list with its name in the following format:
"listname: node0value -> node1value -> node2value ... nodeNvalue -> END"
DBLL_PRINT_LIST(list);
Frees every node in the list.
dbll_destroy_list(list);
- Please don't use this library to create circular linked lists, or any kind of list involving cycles - all the functions would just break down, since they assume a linear structure.
Provided under the MIT License, (C) 2020 Siddharth Singh