8.6. list — Abstract lists

Source code: src/collections/list.h


Defines

LIST_SL_INIT(list_p) do { \ (list_p)->head_p = NULL; \ (list_p)->tail_p = NULL; \ } while (0);

Initialize given singly linked list object.

Parameters
  • list_p: List object to initialize.

LIST_SL_INIT_STRUCT { .head_p = NULL, .tail_p = NULL }
LIST_SL_PEEK_HEAD(list_p, element_pp) do { \ *(element_pp) = (list_p)->head_p; \ } while (0);

Peek at the first element in the list.

Parameters
  • list_p: List object.
  • element_pp: First element of the list.

LIST_SL_ADD_HEAD(list_p, element_p) do { \ (element_p)->next_p = (list_p)->head_p; \ (list_p)->head_p = element_p; \ if ((list_p)->tail_p == NULL) { \ (list_p)->tail_p = (element_p); \ } \ } while (0);

Add given element to the beginning of given list.

Parameters
  • list_p: List object.
  • element_p: Element to add.

LIST_SL_ADD_TAIL(list_p, element_p) do { \ (element_p)->next_p = NULL; \ if ((list_p)->tail_p != NULL) { \ ((struct list_next_t *)((list_p)->tail_p))->next_p = element_p; \ } else if ((list_p)->head_p == NULL) { \ (list_p)->head_p = (element_p); \ } \ (list_p)->tail_p = element_p; \ } while (0);

Add given element to the end of given list.

Parameters
  • list_p: List object.
  • element_p: Element to add.

LIST_SL_REMOVE_HEAD(list_p, element_pp) do { \ *(element_pp) = (list_p)->head_p; \ if (*(element_pp) != NULL) { \ (list_p)->head_p = (void *)(*(element_pp))->next_p; \ (*(element_pp))->next_p = NULL; \ if ((list_p)->tail_p == *(element_pp)) { \ (list_p)->tail_p = NULL; \ } \ } \ } while (0)

Get the first element of given list and then remove it from given list.

Parameters
  • list_p: List object.
  • element_pp: First element of the list.

LIST_SL_ITERATOR_INIT(iterator_p, list_p) do { \ (iterator_p)->next_p = (list_p)->head_p; \ } while (0)

Initialize given iterator object.

Parameters
  • iterator_p: Iterator to initialize.
  • list_p: List object to iterate over.

LIST_SL_ITERATOR_NEXT(iterator_p, element_pp) do { \ *(element_pp) = (iterator_p)->next_p; \ if ((iterator_p)->next_p != NULL) { \ (iterator_p)->next_p = \ ((struct list_sl_iterator_t *)(iterator_p)->next_p)->next_p; \ } \ } while (0)

Get the next element from given iterator object.

Parameters
  • iterator_p: Iterator object.
  • element_pp: Next element of the list.

LIST_SL_REMOVE_ELEM(list_p, iterator_p, element_p, iterator_element_p, previous_element_p) LIST_SL_ITERATOR_INIT((iterator_p), (list_p)); \ (previous_element_p) = NULL; \ while (1) { \ LIST_SL_ITERATOR_NEXT((iterator_p), &(iterator_element_p)); \ if ((iterator_element_p) == NULL) { \ /* Element not found.*/ \ break; \ } else if ((iterator_element_p) == (element_p)) { \ /* Element found. Remove it. */ \ if ((previous_element_p) != NULL) { \ (previous_element_p)->next_p = (element_p)->next_p; \ } else { \ (list_p)->head_p = (void *)((element_p)->next_p); \ } \ if ((element_p)->next_p == NULL) { \ (list_p)->tail_p = NULL; \ } \ break; \ } \ (previous_element_p) = (iterator_element_p); \ }

Remove given element from given list.

Parameters
  • list_p: List object.
  • iterator_p: Used internally.
  • element_p: Element to remove.
  • iterator_element_p: Used internally.
  • previous_element_p: Used internally.

struct list_next_t
#include <list.h>

Public Members

void *next_p
struct list_singly_linked_t

Public Members

void *head_p
void *tail_p
struct list_sl_iterator_t

Public Members

void *next_p