Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef BARRUST_DOUBLY_LINKED_LIST_H__
#define BARRUST_DOUBLY_LINKED_LIST_H__
/*******************************************************************************
***
*** Author: Tyler Barrus
*** email: barrust@gmail.com
***
*** Version: 0.1.1
*** Purpose: Generic doubly linked list implementation
***
*** License: MIT 2019
***
*** URL: https://github.com/barrust/c-utils
***
*** Usage:
*** #include "dllist.h"
*** dllist_t l = dll_init();
*** int i;
*** for (i = 0; i < 5; i++) {
*** int* t = calloc(1, sizeof(int));
*** *t = i;
*** dll_append(l, t);
*** }
*** // loop over the linked list
*** dll_node* n = dll_first_node(l);
*** while (n != NULL) {
*** // do something!
*** n = n->next;
*** }
*** dll_free_alt(l, true);
***
*******************************************************************************/
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct __doubly_linked_list dllist;
typedef struct __doubly_linked_list *dllist_t;
#define DLL_SUCCESS 0
#define DLL_FAILURE -1
typedef struct __dll_node {
void* data;
struct __dll_node* next;
struct __dll_node* prev;
} dll_node;
/* Initialize the doubly linked list
Returns:
NULL - If error allocating the memory
dllist_t
*/
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Free the data from the doubly linked list;
NOTE: does not free the data element */
void dll_free(dllist_t l);
/* Free the data from the doubly linked list;
NOTE: to free the data, set free_data to true */
void dll_free_alt(dllist_t l, bool free_data);
/* Returns the number of nodes in the doubly linked list */
size_t dll_num_elements(dllist_t l);
/* Return the head node from the doubly linked list */
dll_node* dll_first_node(dllist_t l);
/* Return the tail node from the doubly linked list */
dll_node* dll_last_node(dllist_t l);
/* Insert a new node to the end of the doubly linked list */
int dll_append(dllist_t l, void* data);
/* Insert a new node at the position pointed to by `idx` */
int dll_insert(dllist_t l, void* data, int idx);
/* Remove a node that at the position `idx` */
void* dll_remove(dllist_t l, int idx);
void dll_remove_alt(dllist_t l, size_t idx, bool free_data);
/* Traverse the list easily using the following macros */
#define dll_traverse(l, node) for (node = dll_first_node(l); node != NULL; node = node->next)
#define dll_reverse_traverse(l, node) for (node = dll_last_node(l); node != NULL; node = node->prev)
#ifdef __cplusplus
} // extern "C"
#endif
#endif