LibCDS
linked_list.h
Go to the documentation of this file.
1 /**
2  * @file linked_list.h The doubly linked list.
3  */
4 
5 #ifndef _LINKED_LIST_H_
6 #define _LINKED_LIST_H_
7 
8 #include "../util.h"
9 
10 /** LinkedListData is the data type for the container private information. */
11 typedef struct _LinkedListData LinkedListData;
12 
13 /** The implementation for doubly linked list. */
14 typedef struct _LinkedList {
15  /** The container private information */
17 
18  /** Push an item to the head of the list.
19  @see LinkedListPushFront */
20  int32_t (*push_front) (struct _LinkedList*, Item);
21 
22  /** Push an item to the tail of the list.
23  @see LinkedListPushBack */
24  int32_t (*push_back) (struct _LinkedList*, Item);
25 
26  /** Insert an item to the designated index of the list.
27  @see LinkedListInsert */
28  int32_t (*insert) (struct _LinkedList*, Item, int32_t);
29 
30  /** Pop an item from the head of the list.
31  @see LinkedListPopFront */
32  int32_t (*pop_front) (struct _LinkedList*);
33 
34  /** Pop an item from the tail of the list.
35  @see LinkedListPopBack */
36  int32_t (*pop_back) (struct _LinkedList*);
37 
38  /** Delete an item from the designated index of the list
39  @see LinkedListDelete */
40  int32_t (*delete) (struct _LinkedList*, int32_t);
41 
42  /** Replace an item at the head of the list.
43  @see LinkedListSetFront */
44  int32_t (*set_front) (struct _LinkedList*, Item);
45 
46  /** Replace an item at the tail of the list.
47  @see LinkedListSetBack */
48  int32_t (*set_back) (struct _LinkedList*, Item);
49 
50  /** Replace an item at the designated index of the list.
51  @see LinkedListSetAt */
52  int32_t (*set_at) (struct _LinkedList*, Item, int32_t);
53 
54  /** Get an item from the head of the list.
55  @see LinkedListGetFront */
56  int32_t (*get_front) (struct _LinkedList*, Item*);
57 
58  /** Get an item from the tail of the list.
59  @see LinkedListGetBack */
60  int32_t (*get_back) (struct _LinkedList*, Item*);
61 
62  /** Get an item from the designated index of the list.
63  @see LinkedListGetAt */
64  int32_t (*get_at) (struct _LinkedList*, Item*, int32_t);
65 
66  /** Return the number of stored items.
67  @see LinkedListSize */
68  int32_t (*size) (struct _LinkedList*);
69 
70  /** Reverse the list.
71  @see LinkedListReverse */
72  int32_t (*reverse) (struct _LinkedList*);
73 
74  /** Iterate through the list till the tail end.
75  @see LinkedListIterate */
76  int32_t (*iterate) (struct _LinkedList*, bool, Item*);
77 
78  /** Reversely iterate through the list till the head end.
79  @see LinkedListReverseIterate */
80  int32_t (*reverse_iterate) (struct _LinkedList*, bool, Item*);
81 
82  /** Immediately replace the item at a specific iteration.
83  @see LinkedListReplace */
84  int32_t (*replace) (struct _LinkedList*, Item);
85 
86  /** Set the custom item resource clean method.
87  @see LinkedListSetDestroy */
88  int32_t (*set_destroy) (struct _LinkedList*, void (*) (Item));
89 } LinkedList;
90 
91 
92 /*===========================================================================*
93  * Definition for the exported member operations *
94  *===========================================================================*/
95 /**
96  * @brief The constructor for LinkedList.
97  *
98  * @param ppObj The double pointer to the to be constructed list
99  *
100  * @retval SUCC
101  * @retval ERR_NOMEM Insufficient memory for list construction
102  */
103 int32_t LinkedListInit(LinkedList **ppObj);
104 
105 /**
106  * @brief The destructor for LinkedList.
107  *
108  * If the custom resource clean method is set, it also runs the resource clean
109  * method for all the items.
110  *
111  * @param ppObj The double pointer to the to be destructed list
112  */
113 void LinkedListDeinit(LinkedList **ppObj);
114 
115 /**
116  * @brief Push an item to the head of the list.
117  *
118  * @param self The pointer to the LinkedList structure
119  * @param item The designated item
120  *
121  * @retval SUCC
122  * @retval ERR_NOINIT Uninitialized container
123  * @retval ERR_NOMEM Insufficient memory for list extension
124  */
125 int32_t LinkedListPushFront(LinkedList *self, Item item);
126 
127 /**
128  * @brief Push an item to the tail of the list.
129  *
130  * @param self The pointer to the LinkedList structure
131  * @param item The designated item
132  *
133  * @retval SUCC
134  * @retval ERR_NOINIT Uninitialized container
135  * @retval ERR_NOMEM Insufficient memory for list extension
136  */
137 int32_t LinkedListPushBack(LinkedList *self, Item item);
138 
139 /**
140  * @brief Insert an item to the designated index of the list.
141  *
142  * This function inserts an item to the designated index of the list and shifts
143  * the trailing items one position to the tail. The index can be positive or
144  * negative, but its absolute value should be smaller than or equal to the list
145  * size.
146  * The operation supports both forward and backward indexing. For the former one,
147  * the list is traversed from the head to the tail. And for the later one, the
148  * direction is reversed.
149  * To illustrate the behavior, let N denote the list size. Thus for forward indexing
150  * , index (0) and index (N-1) point to the head and tail. And for backward indexing,
151  * index (-1) and index (-N) point to the list tail and head.
152  * But no matter which indexing method is applied, the list always grows from the
153  * head to the tail.
154  *
155  * @param self The pointer to the LinkedList structure
156  * @param item The designated item
157  * @param iIdx The designated index
158  *
159  * @retval SUCC
160  * @retval ERR_NOINIT Uninitialized container
161  * @retval ERR_NOMEM Insufficient memory for list extension
162  * @retval ERR_IDX Out of range indexing
163  *
164  * @note The absolute value of the index should be smaller than or equal to the
165  * list size.
166  */
167 int32_t LinkedListInsert(LinkedList *self, Item item, int32_t iIdx);
168 
169 /**
170  * @brief Pop an item from the head of the list.
171  *
172  * This function removes an item from the head of the list. If the custom
173  * resource clean method is set, it also runs the resource clean method for the
174  * removed item.
175  *
176  * @param self The pointer to the LinkedList structure
177  *
178  * @retval SUCC
179  * @retval ERR_NOINIT Uninitialized container
180  * @retval ERR_IDX Empty list
181  */
182 int32_t LinkedListPopFront(LinkedList *self);
183 
184 /**
185  * @brief Pop an item from the tail of the list.
186  *
187  * This function removes an item from the tail of the list. If the custom
188  * resource clean method is set, it also runs the resource clean method for the
189  * removed item.
190  *
191  * @param self The pointer to the LinkedList structure
192  *
193  * @retval SUCC
194  * @retval ERR_NOINIT Uninitialized container
195  * @retval ERR_IDX Empty list
196  */
197 int32_t LinkedListPopBack(LinkedList *self);
198 
199 /**
200  * @brief Remove an item from the designated index of the list.
201  *
202  * This function removes an item from the designated index of the list and shifts
203  * the trailing items one position to the head. If the custom resource clean
204  * method is set, it also runs the resource clean method for the removed item.
205  * The operation supports both forward and backward indexing. Let N denote the
206  * list size and i denote the index.
207  * For forward indexing, inequality 0 <= i < N must be fitted.
208  * For backward indexing, inequality 0 < i <= -N must be fitted.
209  *
210  * @param self The pointer to the LinkedList structure
211  * @param iIdx The designated index
212  *
213  * @retval SUCC
214  * @retval ERR_NOINIT Uninitialized container
215  * @retval ERR_IDX Out of range indexing
216  */
217 int32_t LinkedListDelete(LinkedList *self, int32_t iIdx);
218 
219 /**
220  * @brief Replace an item at the head of the list.
221  *
222  * This function replaces the head item with the designated one. If the custom
223  * resource clean method is set, it also runs the resource clean method for the
224  * replaced item.
225  *
226  * @param self The pointer to the LinkedList structure
227  * @param item The designated item
228  *
229  * @retval SUCC
230  * @retval ERR_NOINIT Uninitialized container
231  * @retval ERR_IDX Empty list
232  */
233 int32_t LinkedListSetFront(LinkedList *self, Item item);
234 
235 /**
236  * @brief Replace an item at the tail of the list.
237  *
238  * This function replaces the tail item with the designated one. If the custom
239  * resource clean method is set, it also runs the resource clean method for the
240  * replaced item.
241  *
242  * @param self The pointer to the LinkedList structure
243  * @param item The designated item
244  *
245  * @retval SUCC
246  * @retval ERR_NOINIT Uninitialized container
247  * @retval ERR_IDX Empty list
248  */
249 int32_t LinkedListSetBack(LinkedList *self, Item item);
250 
251 /**
252  * @brief Replace an item at the designated index of the list.
253  *
254  * This function replaces the item at the designated index with the designated
255  * one. If the custom resource clean method is set, it also runs the resource
256  * clean method for the replaced item.
257  * The operation supports both forward and backward indexing. Let N denote the
258  * list size and i denote the index.
259  * For forward indexing, inequality 0 <= i < N must be fitted.
260  * For backward indexing, inequality 0 < i <= -N must be fitted.
261  *
262  * @param self The pointer to the LinkedList structure
263  * @param item The designated item
264  * @param iIdx The designated index
265  *
266  * @retval SUCC
267  * @retval ERR_NOINIT Uninitialized container
268  * @retval ERR_IDX Out of range index
269  */
270 int32_t LinkedListSetAt(LinkedList *self, Item item, int32_t iIdx);
271 
272 /**
273  * @brief Get an item from the head of the list.
274  *
275  * @param self The pointer to the LinkedList structure
276  * @param pItem The pointer to the returned item
277  *
278  * @retval SUCC
279  * @retval ERR_NOINIT Uninitialized container
280  * @retval ERR_IDX Empty list
281  * @retval ERR_GET Invalid parameter to store returned item
282  *
283  * @note If the exception occurs, the second parameter will be updated with NULL.
284  */
285 int32_t LinkedListGetFront(LinkedList *self, Item *pItem);
286 
287 /**
288  * @brief Get an item from the tail of the list.
289  *
290  * @param self The pointer to the LinkedList structure
291  * @param pItem The pointer to the returned item
292  *
293  * @retval SUCC
294  * @retval ERR_NOINIT Uninitialized container
295  * @retval ERR_IDX Empty list
296  * @retval ERR_GET Invalid parameter to store returned item
297  *
298  * @note If the exception occurs, the second parameter will be updated with NULL.
299  */
300 int32_t LinkedListGetBack(LinkedList *self, Item *pItem);
301 
302 /**
303  * @brief Get an item from the designated index of the list.
304  *
305  * The operation supports both forward and backward indexing. Let N denote the
306  * list size and i denote the index.
307  * For forward indexing, inequality 0 <= i < N must be fitted.
308  * For backward indexing, inequality 0 < i <= -N must be fitted.
309  *
310  * @param self The pointer to the LinkedList structure
311  * @param pItem The pointer to the returned item
312  * @param iIdx The designated index
313  *
314  * @retval SUCC
315  * @retval ERR_NOINIT Uninitialized container
316  * @retval ERR_IDX Out of range index
317  * @retval ERR_GET Invalid parameter to store returned item
318  *
319  * @note If the exception occurs, the second parameter will be updated with NULL.
320  */
321 int32_t LinkedListGetAt(LinkedList *self, Item *pItem, int32_t iIdx);
322 
323 /**
324  * @brief Return the number of stored items.
325  *
326  * @param self The pointer to the LinkedList structure
327  *
328  * @return The number of stored item
329  * @retval ERR_NOINIT Uninitialized container
330  */
331 int32_t LinkedListSize(LinkedList *self);
332 
333 /**
334  * @brief Reverse the list.
335  *
336  * @param self The pointer to the LinkedList structure
337  *
338  * @retval SUCC
339  * @retval ERR_NOINIT Uninitialized container
340  */
341 int32_t LinkedListReverse(LinkedList *self);
342 
343 /**
344  * @brief Iterate through the list till the tail end.
345  *
346  * Before iterating through the list, it is necessary to pass bReset := true and
347  * pItem := NULL for iterator initialization.
348  * After initialization, you can pass bReset := false and pItem := the relevant
349  * pointer to get the returned item at each iteration.
350  *
351  * @param self The pointer to the LinkedList structure
352  * @param bReset The knob to reset the iteration
353  * @param pItem The pointer to the returned item
354  *
355  * @retval SUCC
356  * @retval END Beyond the tail end
357  * @retval ERR_NOINIT Uninitialized container
358  * @retval ERR_GET Invalid parameter to store returned item
359  */
360 int32_t LinkedListIterate(LinkedList *self, bool bReset, Item *pItem);
361 
362 /**
363  * @brief Reversely iterate through the list till the head end.
364  *
365  * Before reversely iterating through the list, it is necessary to pass
366  * bReset := true and pItem := NULL for iterator initialization.
367  * After initialization, you can pass bReset := false and pItem := the relevant
368  * pointer to get the returned item at each iteration.
369  *
370  * @param self The pointer to the LinkedList structure
371  * @param bReset The knob to reset the iteration
372  * @param pItem The pointer to the returned item
373  *
374  * @retval SUCC
375  * @retval END Beyond the head end
376  * @retval ERR_NOINIT Uninitialized container
377  * @retval ERR_GET Invalid parameter to store returned item
378  */
379 int32_t LinkedListReverseIterate(LinkedList *self, bool bReset, Item *pItem);
380 
381 /**
382  * @brief Immediately replace the item at a specific iteration.
383  *
384  * This operator should be applied within the scope of iterate() or
385  * reverse_iterate() iterators. If the custom resource clean method is set, it
386  * also runs the resource clean method for the replaced item
387  *
388  * @param self The pointer to the LinkedList structure
389  * @param item The item which intends to take position
390  *
391  * @retval SUCC
392  * @retval ERR_NOINIT Uninitialized container
393  * @retval ERR_IDX Out of range indexing
394  *
395  * @note If you intend to use the operator outside of the iterators, the behavior
396  * is undefined. Which is, the operator may successfully replace the item or it
397  * may return an error code.
398  */
399 int32_t LinkedListReplace(LinkedList *self, Item item);
400 
401 /**
402  * @brief Set the custom item resource clean method.
403  *
404  * @param self The pointer to the LinkedList structure
405  * @param pFunc The function pointer to the custom method
406  *
407  * @retval SUCC
408  * @retval ERR_NOINIT Uninitialized container
409  */
410 int32_t LinkedListSetDestroy(LinkedList *self, void (*pFunc) (Item));
411 
412 #endif
int32_t LinkedListPopFront(LinkedList *self)
Pop an item from the head of the list.
int32_t LinkedListPushFront(LinkedList *self, Item item)
Push an item to the head of the list.
int32_t LinkedListInit(LinkedList **ppObj)
The constructor for LinkedList.
int32_t LinkedListInsert(LinkedList *self, Item item, int32_t iIdx)
Insert an item to the designated index of the list.
LinkedListData * pData
The container private information.
Definition: linked_list.h:16
int32_t LinkedListPopBack(LinkedList *self)
Pop an item from the tail of the list.
int32_t LinkedListSetBack(LinkedList *self, Item item)
Replace an item at the tail of the list.
int32_t LinkedListPushBack(LinkedList *self, Item item)
Push an item to the tail of the list.
struct _LinkedListData LinkedListData
LinkedListData is the data type for the container private information.
Definition: linked_list.h:11
int32_t LinkedListSetFront(LinkedList *self, Item item)
Replace an item at the head of the list.
int32_t LinkedListGetAt(LinkedList *self, Item *pItem, int32_t iIdx)
Get an item from the designated index of the list.
int32_t LinkedListSize(LinkedList *self)
Return the number of stored items.
int32_t LinkedListReplace(LinkedList *self, Item item)
Immediately replace the item at a specific iteration.
The implementation for doubly linked list.
Definition: linked_list.h:14
int32_t LinkedListSetAt(LinkedList *self, Item item, int32_t iIdx)
Replace an item at the designated index of the list.
int32_t LinkedListDelete(LinkedList *self, int32_t iIdx)
Remove an item from the designated index of the list.
int32_t LinkedListGetFront(LinkedList *self, Item *pItem)
Get an item from the head of the list.
int32_t LinkedListReverseIterate(LinkedList *self, bool bReset, Item *pItem)
Reversely iterate through the list till the head end.
int32_t LinkedListReverse(LinkedList *self)
Reverse the list.
void LinkedListDeinit(LinkedList **ppObj)
The destructor for LinkedList.
int32_t LinkedListGetBack(LinkedList *self, Item *pItem)
Get an item from the tail of the list.
int32_t LinkedListSetDestroy(LinkedList *self, void(*pFunc)(Item))
Set the custom item resource clean method.
int32_t LinkedListIterate(LinkedList *self, bool bReset, Item *pItem)
Iterate through the list till the tail end.