An implementation of an intrusive circular doubly linked list and an intrusive singly linked list.
- Include
list.h
and compilelist.c
. - Embed a member of type
struct list_element
in your struct that will be a member of a list. To store an object in more than one list at a time it should have anotherstruct list_element
member for each list. If an object will only be in one list at a time then it needs only onestruct list_element
. - Create instances of
struct list
to represent lists. - Use
list_append
and/orlist_prepend
to addstruct list_element
s to your list(s). - Use
struct list_iterator
andlist_iterator_init
/list_iterator_next
to traverse your lists.
- Include
slist.h
and compileslist.c
. - Embed a member of type
struct slist_element
in the struct that will be a member of a list. - Create an instance of
struct slist
. - Add elements to the list with
slist_append
andslist_prepend
. Remove withslist_remove
. - Use
struct slist_iterator
andslist_iterator_init
/slist_iterator_next
to traverse your lists.
#include <stdio.h>
#include "list.h"
#define CONTAINER_OF(ptr, type, field_name) ((type *)(((char *)ptr) - offsetof(type, field_name)))
struct list my_list;
struct my_list_element {
int data;
struct list_element elem;
};
struct my_list_element e0;
struct my_list_element e1;
int main(void) {
//initialize list
list_init(&my_list);
//add elements
e0.data = 0;
list_element_init(&e0.elem);
list_append(&my_list, &e0.elem);
e1.data = 1;
list_element_init(&e1.elem);
list_append(&my_list, &e1.elem);
//iterate
struct list_iterator it;
list_iterator_init(&it, &my_list);
struct list_element *current;
while (list_iterator_next(&it, ¤t) == 0) {
struct my_list_element *current_elem = CONTAINER_OF(current, struct my_list_element, elem);
printf("element with data %d\r\n", current_elem->data);
}
return 0;
}
See test/test_slist.h
for an example of slist.