UYS
0.1
Useful Yet Simple (C libraries collection)
|
Interface for the linked list implementation. More...
#include <stdlib.h>
Go to the source code of this file.
Data Structures | |
struct | ListElmt |
structure for linked list elements. More... | |
struct | List |
structure for linked lists. More... | |
Macros | |
#define | list_size(list) ((list)->size) |
evaluates to the number of elements on list . More... | |
#define | list_head(list) ((list)->head) |
evaluates to the element at the head of list . More... | |
#define | list_tail(list) ((list)->tail) |
: evaluates to the element at the tail of list . More... | |
#define | list_is_head(list, element) ((element) == (list)->head ? 1 : 0) |
determines whether element is at the head of list . More... | |
#define | list_is_tail(element) ((element)->next == NULL ? 1 : 0) |
determines whether element is at the tail of list . More... | |
#define | list_data(element) ((element)->data) |
evaluates to the data stored on element . More... | |
#define | list_next(element) ((element)->next) |
evaluates to the element following element . More... | |
#define | list_is_empty(list) (list->size == 0 ? 1 : 0) |
determines whether list is empty. More... | |
Typedefs | |
typedef struct ListElmt | ListElmt |
structure for linked list elements. | |
typedef struct List | List |
structure for linked lists. | |
Functions | |
void | list_init (List *list, void(*destroy)(void *data)) |
Initializes the linked list specified by list . More... | |
void | list_destroy (List *list) |
Destroys the linked list specified by list . More... | |
int | list_ins_next (List *list, ListElmt *element, const void *data) |
Inserts an element just after element on list . More... | |
int | list_rem_next (List *list, ListElmt *element, void **data) |
Removes the element just after element on list . More... | |
Interface for the linked list implementation.
Definition in file list.h.
#define list_data | ( | element | ) | ((element)->data) |
#define list_head | ( | list | ) | ((list)->head) |
#define list_is_empty | ( | list | ) | (list->size == 0 ? 1 : 0) |
#define list_is_head | ( | list, | |
element | |||
) | ((element) == (list)->head ? 1 : 0) |
#define list_is_tail | ( | element | ) | ((element)->next == NULL ? 1 : 0) |
#define list_next | ( | element | ) | ((element)->next) |
#define list_size | ( | list | ) | ((list)->size) |
evaluates to the number of elements on list
.
Complexity: O(1).
list | pointer to a List struct. |
Definition at line 166 of file list.h.
Referenced by list_destroy(), list_ins_next(), and list_rem_next().
#define list_tail | ( | list | ) | ((list)->tail) |
void list_destroy | ( | List * | list | ) |
Destroys the linked list specified by list
.
No other operations are permitted after calling list_destroy unless list_init is called again. The list_destroy operation removes all elements from a list and calls the function passed as destroy
to list_init once for each element as it is removed, provided destroy was not set to NULL
. Complexity: O(n), where n is the number of elements in the list.
list | pointer to a List struct. |
Definition at line 48 of file list.c.
References list_rem_next(), and list_size.
void list_init | ( | List * | list, |
void(*)(void *data) | destroy | ||
) |
Initializes the linked list specified by list
.
This operation must be called for a linked list before the list can be used with any other operation. The destroy
argument provides a way to free dynamically allocated data when list_destroy
is called Complexity: O(1).
list | pointer to a List struct. |
destroy | pointer to a function to clear allocated memory for data. If the list contains data dynamically allocated using malloc, destroy should be set to free to free the data as the list is destroyed. For structured data containing several dynamically allocated members, destroy should be set to a user-defined function that calls free for each dynamically allocated member as well as for the structure itself. For a list containing data that should not be freed, destroy should be set to NULL . |
list_init
Inserts an element just after element
on list
.
If element
is NULL
, the new element is inserted at the head of the list. The new element contains a pointer to data
, so the memory referenced by data
should remain valid as long as the element remains in the list. It is the responsibility of the caller to manage the storage associated with data
. Complexity: O(1).
list | pointer to a List struct. |
data | pointer to the data that will be referenced by the new element. |
list_ins_next
Definition at line 78 of file list.c.
References list_size.
Removes the element just after element
on list
.
If element
is NULL
, the element at the head of the list is removed. Upon return, data points to the data stored in the element that was removed. It is the responsibility of the caller to manage the storage associated with the data. Complexity: O(1).
list | pointer to a List struct. |
element | pointer to the ListElmt after which the removal should occur. |
**data | pointer to pointer where the removed element's data reference will be stored. |
list_rem_next
Definition at line 126 of file list.c.
References list_size.
Referenced by list_destroy().