Linked Lists

Defines

__HEAD(list)
__TAIL(list)
NEXT_SAFE(current)
linked_list_foreach(list, current)

Advance through a linked list element by element.

linked_list_foreach() should be used like a for loop; for example:

struct element_type * current;
linked_list_foreach(list, current) {
        do_something(current);
}
Do not modify the element’s next pointer inside the body of the loop, or the behavior of linked_list_foreach() is undefined. See linked_list_foreach_safe() instead.
Parameters
  • list: The list to iterate over
  • current: A list element pointer that will point to the current element

linked_list_foreach_i(list, current, i)

Advance through a linked list element by element with a counter.

linked_list_foreach_i() should be used like a for loop; for example:

int i;
struct element_type * current;
linked_list_foreach(list, current, i) {
        if(some_condition(i)) {
                do_something(current);
        }
}
Parameters
  • list: The list to iterate over
  • current: A list element pointer that will point to the current element
  • i: The numeric counter to increment on each iteration

Do not modify the element’s next pointer inside the body of the loop, or the behavior of linked_list_foreach() is undefined. See linked_list_foreach_i_safe() instead.

linked_list_foreach_safe(list, current)

Advance through a linked list element by element.

The syntax of linked_list_foreach_safe() is the same as linked_list_foreach(). However, it is safe to change the next pointer in the current element during the loop body.

Parameters
  • list: The list to iterate over
  • current: A list element pointer that will point to the current element

linked_list_foreach_i_safe(list, current, i)

Advance through a linked list element by element with a counter.

The syntax of linked_list_foreach_safe() is the same as linked_list_foreach_i(). However, it is safe to change the next pointer in the current element during the loop body.

Parameters
  • list: The list to iterate over
  • current: A list element pointer that will point to the current element
  • i: The numeric counter to increment on each iteration

linked_list_while(list, current, condition)

Advance through a linked list while some condition is true.

linked_list_while() should be used like a while loop; for example:

struct element_type * current;
linked_list_while(list, current, current->data != NULL) {
        do_something(current->data);
}
Do not modify the element’s next pointer inside the body of the loop, or the behavior of linked_list_while() is undefined. See linked_list_while_safe() instead.
Parameters
  • list: The list to iterate over
  • current: A list element pointer that will point to the current element
  • condition: A condition that will determine whether to continue iterating

linked_list_while_safe(list, current, condition)

Advance through a linked list element by element.

The syntax of linked_list_while_safe() is the same as linked_list_while(). However, it is safe to change the next pointer in the current element during the loop body.

Parameters
  • list: The list to iterate over
  • current: A list element pointer that will point to the current element
  • condition: A condition that will determine whether to continue iterating

otherwise(current)

Run a block of code if the preceeding loop did not break.

otherwise() should directly follow a linklist loop in the same way an else statement follows an if block. It’s function is the same as the for/else construction in Python. If the preceeding loop advanced all the way through the list and did not break or exit the loop on any element, the otherwise block will be called. For example:

linked_list_foreach(list, current) {
        if(!current->data)
                break;
} otherwise(current) {
        puts("No NULL data elements.");
}
Parameters
  • current: The same current element used in the preceeding loop.