CIRCLEQ_FOREACH_REVERSE - Linux
Overview
The CIRCLEQ_FOREACH_REVERSE
macro iterates over a circular doubly linked list in reverse order. It is commonly used to traverse the list from the last element to the first.
Syntax
#include <sys/queue.h>
CIRCLEQ_FOREACH_REVERSE(var, head, field)
Parameters
- var: A variable to store each entry found in the list as it is iterated.
- head: The head element of the circular doubly linked list.
- field: The name of the field within each entry that contains the next entry in the list.
Options/Flags
There are no options or flags available for CIRCLEQ_FOREACH_REVERSE
.
Examples
Iterating Over a List in Reverse Order
The following example shows how to iterate over a circular doubly linked list in reverse order:
#include <sys/queue.h>
struct entry {
CIRCLEQ_ENTRY(entry) entries;
int value;
};
int main() {
struct entry *head = NULL;
CIRCLEQ_INSERT_HEAD(head, entry1, entries);
CIRCLEQ_INSERT_HEAD(head, entry2, entries);
CIRCLEQ_INSERT_HEAD(head, entry3, entries);
CIRCLEQ_FOREACH_REVERSE(entry, head, entries) {
printf("%d\n", entry->value);
}
return 0;
}
Output:
3
2
1
Common Issues
One common issue when using CIRCLEQ_FOREACH_REVERSE
is ensuring that the circular doubly linked list is properly initialized. The head element must be set to NULL
before inserting any entries into the list.
Integration
CIRCLEQ_FOREACH_REVERSE
can be used in conjunction with other circular doubly linked list macros, such as CIRCLEQ_INSERT_HEAD
and CIRCLEQ_REMOVE
, to manipulate circular doubly linked lists efficiently.
Related Commands
CIRCLEQ_FOREACH
: Iterates over a circular doubly linked list in forward order.CIRCLEQ_INIT
: Initializes a circular doubly linked list.CIRCLEQ_INSERT_HEAD
: Inserts a new entry at the head of a circular doubly linked list.CIRCLEQ_REMOVE
: Removes an entry from a circular doubly linked list.