C Tool Box 0.1.0
Loading...
Searching...
No Matches
ctb_DList.h
Go to the documentation of this file.
1/**
2 * @file
3 */
4#ifndef CTB_DLIST_H
5#define CTB_DLIST_H
6
7/**
8 * @brief Doubly linked list type
9 */
10typedef struct ctb_DList ctb_DList_t;
11
12#include "ctb_DListIterator.h"
13#include "ctb_DNode.h"
14#include <stdbool.h>
15#include <stddef.h>
16
17/**
18 * @brief Doubly linked list data structure
19 */
20struct ctb_DList {
21 ctb_DNode_t * first; /**< Pointer to the first node */
22 ctb_DNode_t * last; /**< Pointer to the last node */
23 size_t size; /**< Number of nodes */
24};
25
26/**
27 * @brief Initializes a doubly linked list
28 *
29 * @param self Pointer to a doubly linked list
30 * @return Pointer to the initialized doubly linked list
31 */
33
34/**
35 * @brief Adds a node at the specified index
36 *
37 * @param self Pointer to a doubly linked list
38 * @param node Pointer to the node to add
39 * @param index Index where to add the node
40 */
42 ctb_DList_t * const self, ctb_DNode_t * const node, size_t const index
43);
44
45/**
46 * @brief Adds a node at the beginning
47 *
48 * @param self Pointer to a doubly linked list
49 * @param node Pointer to the node to add
50 */
52 ctb_DList_t * const self, ctb_DNode_t * const node
53);
54
55/**
56 * @brief Adds a node at the end
57 *
58 * @param self Pointer to a doubly linked list
59 * @param node Pointer to the node to add
60 */
61void ctb_DList_addLast(ctb_DList_t * const self, ctb_DNode_t * const node);
62
63/**
64 * @brief Returns the node at the specified index
65 *
66 * @param self Pointer to a doubly linked list
67 * @param index Index of the node to return
68 * @return Pointer to the node at the specified index
69 */
70ctb_DNode_t * ctb_DList_getByIndex(ctb_DList_t * const self, size_t index);
71
72/**
73 * @brief Returns the first node
74 *
75 * @param self Pointer to a doubly linked list
76 * @return Pointer to the first node
77 */
79
80/**
81 * @brief Returns the last node
82 *
83 * @param self Pointer to a doubly linked list
84 * @return Pointer to the last node
85 */
87
88/**
89 * @brief Removes the node at the specified index
90 *
91 * @param self Pointer to a doubly linked list
92 * @param index Index of the node to remove
93 * @return Pointer to the removed node
94 */
96 ctb_DList_t * const self, size_t const index
97);
98
99/**
100 * @brief Removes the first node
101 *
102 * @param self Pointer to a doubly linked list
103 * @return Pointer to the removed node
104 */
106
107/**
108 * @brief Removes the last node
109 *
110 * @param self Pointer to a doubly linked list
111 * @return Pointer to the removed node
112 */
114
115/**
116 * @brief Removes a node
117 *
118 * @param self Pointer to a doubly linked list
119 * @param node Pointer to the node to remove
120 * @return Pointer to the removed node
121 */
123 ctb_DList_t * const self, ctb_DNode_t * const node
124);
125
126/**
127 * @brief Returns the index of a node
128 *
129 * @param self Pointer to a doubly linked list
130 * @param node Pointer to the node
131 * @return Index of the node, or SIZE_MAX if the node is not found
132 */
134 ctb_DList_t * const self, ctb_DNode_t const * const node
135);
136
137/**
138 * @brief Checks if the doubly linked list is empty
139 *
140 * @param self Pointer to a doubly linked list
141 * @return True if the doubly linked list is empty, false otherwise
142 */
143bool ctb_DList_isEmpty(ctb_DList_t * const self);
144
145/**
146 * @brief Returns the size of the doubly linked list
147 *
148 * @param self Pointer to a doubly linked list
149 * @return Size of the doubly linked list
150 */
151size_t ctb_DList_getSize(ctb_DList_t * const self);
152
153/**
154 * @brief Clears the doubly linked list
155 *
156 * @param self Pointer to a doubly linked list
157 */
158void ctb_DList_clear(ctb_DList_t * const self);
159
160/**
161 * @brief Applies an operation to each node of the doubly linked list
162 *
163 * @param self Pointer to a doubly linked list
164 * @param operation Operation to apply to each node
165 */
167 ctb_DList_t * const self,
169);
170
171/**
172 * @brief Finds a node in the doubly linked list that matches a predicate
173 *
174 * @param self Pointer to a doubly linked list
175 * @param predicate Predicate to match a node
176 * @return Pointer to the first node that matches the predicate, or NULL if no
177 * node matches
178 */
180 ctb_DList_t * const self,
182);
183
184#endif // CTB_DLIST_H
void(* ctb_DListIterator_ForEachOperation_t)(ctb_DNode_t *const node)
Typedef for operation function to be applied to each node in the doubly linked list.
Definition ctb_DListIterator.h:28
bool(* ctb_DListIterator_FindPredicate_t)(ctb_DNode_t *const node)
Typedef for predicate function used to find a node in the doubly linked list.
Definition ctb_DListIterator.h:21
ctb_DNode_t * ctb_DList_remove(ctb_DList_t *const self, ctb_DNode_t *const node)
Removes a node.
Definition ctb_DList.c:126
ctb_DNode_t * ctb_DList_removeLast(ctb_DList_t *const self)
Removes the last node.
Definition ctb_DList.c:121
size_t ctb_DList_getSize(ctb_DList_t *const self)
Returns the size of the doubly linked list.
Definition ctb_DList.c:163
void ctb_DList_addFirst(ctb_DList_t *const self, ctb_DNode_t *const node)
Adds a node at the beginning.
Definition ctb_DList.c:43
void ctb_DList_addLast(ctb_DList_t *const self, ctb_DNode_t *const node)
Adds a node at the end.
Definition ctb_DList.c:50
ctb_DNode_t * ctb_DList_find(ctb_DList_t *const self, ctb_DListIterator_FindPredicate_t const predicate)
Finds a node in the doubly linked list that matches a predicate.
Definition ctb_DList.c:186
ctb_DNode_t * ctb_DList_removeByIndex(ctb_DList_t *const self, size_t const index)
Removes the node at the specified index.
Definition ctb_DList.c:87
void ctb_DList_clear(ctb_DList_t *const self)
Clears the doubly linked list.
Definition ctb_DList.c:169
ctb_DNode_t * ctb_DList_getFirst(ctb_DList_t *const self)
Returns the first node.
Definition ctb_DList.c:75
ctb_DList_t * ctb_DList_init(ctb_DList_t *const self)
Initializes a doubly linked list.
Definition ctb_DList.c:7
ctb_DNode_t * ctb_DList_getByIndex(ctb_DList_t *const self, size_t index)
Returns the node at the specified index.
Definition ctb_DList.c:57
ctb_DNode_t * ctb_DList_getLast(ctb_DList_t *const self)
Returns the last node.
Definition ctb_DList.c:81
ctb_DNode_t * ctb_DList_removeFirst(ctb_DList_t *const self)
Removes the first node.
Definition ctb_DList.c:116
void ctb_DList_forEach(ctb_DList_t *const self, ctb_DListIterator_ForEachOperation_t const operation)
Applies an operation to each node of the doubly linked list.
Definition ctb_DList.c:176
bool ctb_DList_isEmpty(ctb_DList_t *const self)
Checks if the doubly linked list is empty.
Definition ctb_DList.c:157
size_t ctb_DList_getIndexOf(ctb_DList_t *const self, ctb_DNode_t const *const node)
Returns the index of a node.
Definition ctb_DList.c:136
void ctb_DList_addByIndex(ctb_DList_t *const self, ctb_DNode_t *const node, size_t const index)
Adds a node at the specified index.
Definition ctb_DList.c:15
Doubly linked list data structure.
Definition ctb_DList.h:20
size_t size
Definition ctb_DList.h:23
ctb_DNode_t * last
Definition ctb_DList.h:22
ctb_DNode_t * first
Definition ctb_DList.h:21
Doubly linked node data structure.
Definition ctb_DNode.h:15