cu
list.h File Reference
#include <stdbool.h>
#include <stdlib.h>

Go to the source code of this file.

Data Structures

struct  listnode_t
 
struct  list_t
 Holds the reference to the head and tail of the list. More...
 

Macros

#define LIST_FOR(IT, L)
 Iterate over the content of the list_t. More...
 
#define LIST_INIT
 
#define LIST_DEL(L, FN)
 Clear and delete a list_t, which was created on the stack. More...
 

Functions

list_tlist_new ()
 Create a new list. More...
 
void * list_clone (const list_t *li, void *(*clone)(void *))
 Clone given list. More...
 
void list_clear (const list_t *li, void(*df)(void *))
 Remove all elements from the list. More...
 
void list_del (list_t *li)
 Free the memory allocated. More...
 
void list_clear_del (list_t *li, void(*df)(void *))
 Remove all elements from the list and free it. More...
 
void list_push_back (list_t *li, void *data)
 Append an element to the list. More...
 
void list_push_front (list_t *li, void *data)
 Prepend an element to the list. More...
 
void list_insert (list_t *li, const size_t index, void *data)
 
void list_append (list_t *li, list_t *other)
 Append another list_t to the current one. More...
 
bool list_is_empty (const list_t *li)
 Check whether the list is empty. More...
 
size_t list_len (const list_t *li)
 Retrieve the length of a list. More...
 
void list_foreach (const list_t *li, void(*f)(void *))
 Apply a function to every element of the list. More...
 
void * list_filter (const list_t *li, const void *key, bool(*pred)(const void *, const void *))
 Filter elements by given predicate. More...
 
void * list_find (const list_t *li, const void *key, bool(*pred)(const void *, const void *))
 Find a element in the list. More...
 
bool list_contains (list_t *li, const void *key, int(*pred)(const void *, const void *))
 

Detailed Description

Interface of a dopply linked list

Macro Definition Documentation

#define LIST_DEL (   L,
  FN 
)
Value:
list_clear(&L, FN); struct listnode_t *IT = NULL; \
for (IT = L.head; IT != NULL; IT = IT->next) { \
free(IT->prev); \
} \
free(L.tail);
struct listnode_t * prev
Definition: list.h:21
struct listnode_t * next
Definition: list.h:19
Definition: list.h:15
void list_clear(const list_t *li, void(*df)(void *))
Remove all elements from the list.

Clear and delete a list_t, which was created on the stack.

Parameters
Llist_t to delete
FNfunction with which the content of the list can be deleted
#define LIST_FOR (   IT,
 
)
Value:
struct listnode_t *IT = NULL; for (IT = L->head; \
IT != NULL; \
IT = IT->next)
struct listnode_t * next
Definition: list.h:19
Definition: list.h:15

Iterate over the content of the list_t.

Parameters
ITname of the temporary variable
Llist_t to iterate over
Returns
yields a element of the list_t, will be accesable through IT->data
#define LIST_INIT
Value:
{.head = NULL, \
.tail = NULL, \
.size = 0}

create a list_t on the stack

Function Documentation

void list_append ( list_t li,
list_t other 
)

Append another list_t to the current one.

Parameters
lilist to append to
otherother list
void list_clear ( const list_t li,
void(*)(void *)  df 
)

Remove all elements from the list.

Parameters
lilist to clear
dfdeleting function, in case of NULL the argument is ignored
void list_clear_del ( list_t li,
void(*)(void *)  df 
)

Remove all elements from the list and free it.

Parameters
liList to clear and delete
dfDeleting function, in case of NULL the argument is ignored
void* list_clone ( const list_t li,
void *(*)(void *)  clone 
)

Clone given list.

Parameters
lilist to clone
clonefunction for given element. In case of NULL it is ignored
Returns
a new list_t
bool list_contains ( list_t li,
const void *  key,
int(*)(const void *, const void *)  pred 
)

Check whether the list contains given key

Parameters
lilist to search
keykey to check
predpredicate function
Returns
returns true if the value is in the list, false otherwise
void list_del ( list_t li)

Free the memory allocated.

Parameters
lilist to delete
void* list_filter ( const list_t li,
const void *  key,
bool(*)(const void *, const void *)  pred 
)

Filter elements by given predicate.

Parameters
lilist to filter
keykey to find
predpredicate function
Returns
returns a new list_t which contains all elements that returned true
void* list_find ( const list_t li,
const void *  key,
bool(*)(const void *, const void *)  pred 
)

Find a element in the list.

Parameters
lilist to search
keykey to find
predpredicate function
Returns
returns a pointer to the element
void list_foreach ( const list_t li,
void(*)(void *)  f 
)

Apply a function to every element of the list.

Parameters
lilist
ffunction to apply
void list_insert ( list_t li,
const size_t  index,
void *  data 
)

Insert data into the list at given position

Parameters
lilist to insert data to
indextarget index
datadata to insert
bool list_is_empty ( const list_t li)

Check whether the list is empty.

Parameters
lilist to check
Returns
returns true on success and false on failure
size_t list_len ( const list_t li)

Retrieve the length of a list.

Parameters
lilist to check
Returns
returns the list length
list_t* list_new ( )

Create a new list.

Returns
a new list_t
void list_push_back ( list_t li,
void *  data 
)

Append an element to the list.

Parameters
lilist to append to
datadata to append
void list_push_front ( list_t li,
void *  data 
)

Prepend an element to the list.

Parameters
lilist to prepend to
datadata to prepend