UYS  0.1
Useful Yet Simple (C libraries collection)
 All Data Structures Files Functions Typedefs Macros Pages
list.h File Reference

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...
 

Detailed Description

Interface for the linked list implementation.

Definition in file list.h.

Macro Definition Documentation

#define list_data (   element)    ((element)->data)

evaluates to the data stored on element.

Parameters
*elementpointer to the ListElmt to be checked.
Returns
Data stored in the element.

Definition at line 237 of file list.h.

#define list_head (   list)    ((list)->head)

evaluates to the element at the head of list.

Complexity: O(1).

Parameters
*listpointer to a List struct.
Returns
Element at the head of the list.

Definition at line 181 of file list.h.

#define list_is_empty (   list)    (list->size == 0 ? 1 : 0)

determines whether list is empty.

Parameters
*listpointer to a List struct.
Returns
1 if the list is empty, or 0 otherwise.

Definition at line 263 of file list.h.

#define list_is_head (   list,
  element 
)    ((element) == (list)->head ? 1 : 0)

determines whether element is at the head of list.

Parameters
*listpointer to a List struct.
*elementpointer to the ListElmt to be checked.
Returns
1 if the element is at the head of the list, or 0 otherwise.

Definition at line 210 of file list.h.

#define list_is_tail (   element)    ((element)->next == NULL ? 1 : 0)

determines whether element is at the tail of list.

Parameters
*listpointer to a List struct.
*elementpointer to the ListElmt to be checked.
Returns
1 if the element is at the tail of the list, or 0 otherwise.

Definition at line 224 of file list.h.

#define list_next (   element)    ((element)->next)

evaluates to the element following element.

Parameters
*elementpointer to a ListElmt struct.
Returns
Element following element.

Definition at line 250 of file list.h.

#define list_size (   list)    ((list)->size)

evaluates to the number of elements on list.

Complexity: O(1).

Parameters
listpointer to a List struct.
Returns
Number of elements in the list.

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)

: evaluates to the element at the tail of list.

Complexity: O(1).

Parameters
*listpointer to a List struct.
Returns
Element at the tail of the list.

Definition at line 196 of file list.h.

Function Documentation

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.

Parameters
listpointer to a List struct.
Returns
None.

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).

Parameters
listpointer to a List struct.
destroypointer 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.
Returns
None.

list_init

Definition at line 31 of file list.c.

int list_ins_next ( List list,
ListElmt element,
const void *  data 
)

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).

Parameters
listpointer to a List struct.
datapointer to the data that will be referenced by the new element.
Returns
0 if inserting the element is successful, or –1 otherwise.

list_ins_next

Definition at line 78 of file list.c.

References list_size.

int list_rem_next ( List list,
ListElmt element,
void **  data 
)

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).

Parameters
listpointer to a List struct.
elementpointer to the ListElmt after which the removal should occur.
**datapointer to pointer where the removed element's data reference will be stored.
Returns
0 if removing the element is successful, or –1 otherwise.

list_rem_next

Definition at line 126 of file list.c.

References list_size.

Referenced by list_destroy().