Intended to work with a list sentinal which is created as an empty list. Insert & delete are O(1).
Data Structures | |
struct | simple_node |
Defines | |
#define | remove_from_list(elem) |
Remove an element from list. | |
#define | insert_at_head(list, elem) |
Insert an element to the list head. | |
#define | insert_at_tail(list, elem) |
Insert an element to the list tail. | |
#define | move_to_head(list, elem) |
Move an element to the list head. | |
#define | move_to_tail(list, elem) |
Move an element to the list tail. | |
#define | make_empty_list(sentinal) |
Make a empty list empty. | |
#define | first_elem(list) ((list)->next) |
Get list first element. | |
#define | last_elem(list) ((list)->prev) |
Get list last element. | |
#define | next_elem(elem) ((elem)->next) |
Get next element. | |
#define | prev_elem(elem) ((elem)->prev) |
Get previous element. | |
#define | at_end(list, elem) ((elem) == (list)) |
Test whether element is at end of the list. | |
#define | is_empty_list(list) ((list)->next == (list)) |
Test if a list is empty. | |
#define | foreach(ptr, list) for( ptr=(list)->next ; ptr!=list ; ptr=(ptr)->next ) |
Walk through the elements of a list. | |
#define | foreach_s(ptr, t, list) for(ptr=(list)->next,t=(ptr)->next; list != ptr; ptr=t, t=(t)->next) |
Walk through the elements of a list. |
#define at_end | ( | list, | |||
elem | ) | ((elem) == (list)) |
Test whether element is at end of the list.
list | list. | |
elem | element. |
#define first_elem | ( | list | ) | ((list)->next) |
Get list first element.
list | list. |
#define foreach | ( | ptr, | |||
list | ) | for( ptr=(list)->next ; ptr!=list ; ptr=(ptr)->next ) |
Walk through the elements of a list.
ptr | pointer to the current element. | |
list | list. |
for
loop. #define foreach_s | ( | ptr, | |||
t, | |||||
list | ) | for(ptr=(list)->next,t=(ptr)->next; list != ptr; ptr=t, t=(t)->next) |
Walk through the elements of a list.
Same as foreach but lets you unlink the current value during a list traversal. Useful for freeing a list, element by element.
ptr | pointer to the current element. | |
t | temporary pointer. | |
list | list. |
for
loop. #define insert_at_head | ( | list, | |||
elem | ) |
Value:
do { \ (elem)->prev = list; \ (elem)->next = (list)->next; \ (list)->next->prev = elem; \ (list)->next = elem; \ } while(0)
list | list. | |
elem | element to insert. |
#define insert_at_tail | ( | list, | |||
elem | ) |
Value:
do { \ (elem)->next = list; \ (elem)->prev = (list)->prev; \ (list)->prev->next = elem; \ (list)->prev = elem; \ } while(0)
list | list. | |
elem | element to insert. |
#define is_empty_list | ( | list | ) | ((list)->next == (list)) |
Test if a list is empty.
list | list. |
#define last_elem | ( | list | ) | ((list)->prev) |
Get list last element.
list | list. |
#define make_empty_list | ( | sentinal | ) |
Value:
do { \ (sentinal)->next = sentinal; \ (sentinal)->prev = sentinal; \ } while (0)
sentinal | list (sentinal element). |
#define move_to_head | ( | list, | |||
elem | ) |
Value:
do { \ remove_from_list(elem); \ insert_at_head(list, elem); \ } while (0)
list | list. | |
elem | element to move. |
#define move_to_tail | ( | list, | |||
elem | ) |
Value:
do { \ remove_from_list(elem); \ insert_at_tail(list, elem); \ } while (0)
list | list. | |
elem | element to move. |
#define next_elem | ( | elem | ) | ((elem)->next) |
Get next element.
elem | element. |
#define prev_elem | ( | elem | ) | ((elem)->prev) |
Get previous element.
elem | element. |
#define remove_from_list | ( | elem | ) |
Value:
do { \ (elem)->next->prev = (elem)->prev; \ (elem)->prev->next = (elem)->next; \ } while (0)
elem | element to remove. |