UYS  0.1
Useful Yet Simple (C libraries collection)
 All Data Structures Files Functions Typedefs Macros Pages
list.h
Go to the documentation of this file.
1 /*****************************************************************
2  *
3  * list.h
4  *
5  * Source: "Mastering Algorithms with C" (1999, p. 56) by Kyle Loudon,
6  * published by O'Reilly & Associates.
7  *
8  * This source can be freely used, adapted, and redistributed in source or
9  * binary form, so long as an acknowledgment appears in derived source files.
10  * The citation should list that the code comes from the book "Mastering
11  * Algorithms with C" by Kyle Loudon, published by O'Reilly & Associates.
12  * This code is under copyright and cannot be included in any other book,
13  * publication, or educational product without permission from O'Reilly
14  * & Associates. No warranty is attached; we cannot take responsibility for
15  * errors or fitness for use.
16  *
17  *****************************************************************/
18 
19 
20 /*****************************************************************
21  * NOTE: Set \file so that Doxygen looks for globals
22  ****************************************************************/
23 /*************************************************************//**
24  * \file list.h
25  * \brief Interface for the linked list implementation.
26  ****************************************************************/
27 
28 
29 #ifndef LIST_H
30 #define LIST_H
31 
32 
33 #include <stdlib.h>
34 
35 
36 /*****************************************************************
37  * struct ListElmt, ListElmt
38  ****************************************************************/
39 typedef struct ListElmt ListElmt;
40 /*************************************************************//**
41  * \brief structure for linked list elements.
42  ****************************************************************/
43 typedef struct ListElmt {
44  void *data;
45  ListElmt *next;
46 } ListElmt;
47 
48 
49 /*****************************************************************
50  * struct List, List
51  ****************************************************************/
52 typedef struct List List;
53 /*************************************************************//**
54  * \brief structure for linked lists.
55  ****************************************************************/
56 typedef struct List {
57  int size;
58  int (*match)(const void *key1, const void *key2);
59  void (*destroy)(void *data);
60  ListElmt *head;
61  ListElmt *tail;
62 } List;
63 
64 
65 /*****************************************************************
66  * list_init
67  ****************************************************************/
68 /*************************************************************//**
69  * \brief Initializes the linked list specified by `list`.
70  *
71  * This operation must be called for a linked list before the list can be
72  * used with any other operation. The `destroy` argument provides a way to
73  * free dynamically allocated data when `list_destroy` is called
74  * Complexity: O(1).
75  *
76  * \param list pointer to a `List` struct.
77  * \param destroy pointer to a function to clear allocated memory for data.
78  * If the list contains data dynamically allocated using malloc, destroy
79  * should be set to `free` to free the data as the list is destroyed.
80  * For structured data containing several dynamically allocated members,
81  * destroy should be set to a user-defined function that calls free for
82  * each dynamically allocated member as well as for the structure itself.
83  * For a list containing data that should not be freed, destroy should be
84  * set to `NULL`.
85  *
86  * \return None.
87  ****************************************************************/
88 void list_init(List *list, void (*destroy)(void *data));
89 
90 
91 /*****************************************************************
92  * list_destroy
93  ****************************************************************/
94 /*************************************************************//**
95  * \brief Destroys the linked list specified by `list`.
96  *
97  * No other operations are permitted after calling list_destroy unless
98  * list_init is called again. The list_destroy operation removes all elements
99  * from a list and calls the function passed as `destroy` to list_init once
100  * for each element as it is removed, provided destroy was not set to `NULL`.
101  * Complexity: O(n), where n is the number of elements in the list.
102  *
103  * \param list pointer to a `List` struct.
104  *
105  * \return None.
106  ****************************************************************/
107 void list_destroy(List *list);
108 
109 
110 /*****************************************************************
111  * list_ins_next
112  ****************************************************************/
113 /*************************************************************//**
114  * \brief Inserts an element just after `element` on `list`.
115  *
116  * If `element` is `NULL`, the new element is inserted at the head of the
117  * list. The new element contains a pointer to `data`, so the memory
118  * referenced by `data` should remain valid as long as the element remains in
119  * the list. It is the responsibility of the caller to manage the storage
120  * associated with `data`.
121  * Complexity: O(1).
122  *
123  * \param list pointer to a `List` struct.
124  * \param data pointer to the data that will be referenced by the new element.
125  *
126  * \return 0 if inserting the element is successful, or –1 otherwise.
127  ****************************************************************/
128 int list_ins_next(List *list, ListElmt *element, const void *data);
129 
130 
131 /*****************************************************************
132  * list_rem_next
133  ****************************************************************/
134 /*************************************************************//**
135  * \brief Removes the element just after `element` on `list`.
136  *
137  * If `element` is `NULL`, the element at the head of the list is removed.
138  * Upon return, data points to the data stored in the element that was
139  * removed. It is the responsibility of the caller to manage the storage
140  * associated with the data.
141  * Complexity: O(1).
142  *
143  * \param list pointer to a `List` struct.
144  * \param element pointer to the `ListElmt` after which the removal should
145  * occur.
146  * \param **data pointer to pointer where the removed element's data
147  * reference will be stored.
148  *
149  * \return 0 if removing the element is successful, or –1 otherwise.
150  ****************************************************************/
151 int list_rem_next(List *list, ListElmt *element, void **data);
152 
153 
154 /*****************************************************************
155  * list_size
156  ****************************************************************/
157 /*************************************************************//**
158  * \brief evaluates to the number of elements on `list`.
159  *
160  * Complexity: O(1).
161  *
162  * \param list pointer to a `List` struct.
163  *
164  * \return Number of elements in the list.
165  ****************************************************************/
166 #define list_size(list)((list)->size)
167 
168 
169 /*****************************************************************
170  * list_head
171  ****************************************************************/
172 /*************************************************************//**
173  * \brief evaluates to the element at the head of `list`.
174  *
175  * Complexity: O(1).
176  *
177  * \param *list pointer to a `List` struct.
178  *
179  * \return Element at the head of the list.
180  ****************************************************************/
181 #define list_head(list) ((list)->head)
182 
183 
184 /*****************************************************************
185  * list_tail
186  ****************************************************************/
187 /*************************************************************//**
188  * \brief: evaluates to the element at the tail of `list`.
189  *
190  * Complexity: O(1).
191  *
192  * \param *list pointer to a `List` struct.
193  *
194  * \return Element at the tail of the list.
195  ****************************************************************/
196 #define list_tail(list)((list)->tail)
197 
198 
199 /*****************************************************************
200  * list_is_head
201  ****************************************************************/
202 /*************************************************************//**
203  * \brief determines whether `element` is at the head of `list`.
204  *
205  * \param *list pointer to a `List` struct.
206  * \param *element pointer to the `ListElmt` to be checked.
207  *
208  * \return 1 if the element is at the head of the list, or 0 otherwise.
209  ****************************************************************/
210 #define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
211 
212 
213 /*****************************************************************
214  * list_is_tail
215  ****************************************************************/
216 /*************************************************************//**
217  * \brief determines whether `element` is at the tail of `list`.
218  *
219  * \param *list pointer to a `List` struct.
220  * \param *element pointer to the `ListElmt` to be checked.
221  *
222  * \return 1 if the element is at the tail of the list, or 0 otherwise.
223  ****************************************************************/
224 #define list_is_tail(element) ((element)->next == NULL ? 1 : 0)
225 
226 
227 /*****************************************************************
228  * list_data
229  ****************************************************************/
230 /*************************************************************//**
231  * \brief evaluates to the data stored on `element`.
232  *
233  * \param *element pointer to the `ListElmt` to be checked.
234  *
235  * \return Data stored in the element.
236  ****************************************************************/
237 #define list_data(element) ((element)->data)
238 
239 
240 /*****************************************************************
241  * list_next
242  ****************************************************************/
243 /*************************************************************//**
244  * \brief evaluates to the element following `element`.
245  *
246  * \param *element pointer to a `ListElmt` struct.
247  *
248  * \return Element following `element`.
249  ****************************************************************/
250 #define list_next(element) ((element)->next)
251 
252 
253 /*****************************************************************
254  * list_is_empty
255  ****************************************************************/
256 /*************************************************************//**
257  * \brief determines whether `list` is empty.
258  *
259  * \param *list pointer to a `List` struct.
260  *
261  * \return 1 if the list is empty, or 0 otherwise.
262  ****************************************************************/
263 #define list_is_empty(list) (list->size == 0 ? 1 : 0)
264 
265 
266 #endif
struct List List
structure for linked lists.
Definition: list.h:52
int list_ins_next(List *list, ListElmt *element, const void *data)
Inserts an element just after element on list.
Definition: list.c:78
void list_destroy(List *list)
Destroys the linked list specified by list.
Definition: list.c:48
void list_init(List *list, void(*destroy)(void *data))
Initializes the linked list specified by list.
Definition: list.c:31
struct ListElmt ListElmt
structure for linked list elements.
Definition: list.h:39
int list_rem_next(List *list, ListElmt *element, void **data)
Removes the element just after element on list.
Definition: list.c:126
structure for linked lists.
Definition: list.h:56
structure for linked list elements.
Definition: list.h:43