ListForeachReverseIdx

Table of Contents

ListForeachReverseIdx

Description

Iterate over each element var of the given list l in reverse order, with index idx. The variable var is declared and defined by this macro. Iteration starts from the tail and moves backward using the prev pointers. The variable idx will contain the zero-based index from the head: index length-1 corresponds to the tail, length-2 to the previous node, and so on down to 0 (head).

Info

The macro supports iteration using relative traversal if random access is required. This means user code can change idx to any value in list boundaries and the macro will adjust node automatically for the new index.

Parameters

NameDirectionDescription
linList to iterate over.
varoutName of the variable to hold the value during iteration.
idxoutVariable that will track the index from the head.

Usage example (Cross-references)

    if (list->length > 0) {
    int sum = 0;
    ListForeachReverseIdx(list, item, idx) {
    sum += item + (int)idx;
    }

Share :

Related Posts

ListForeachIdx

ListForeachIdx Description Iterate over each element var of the given list l, with index idx. The variable var is declared and defined by this macro. Iteration happens in forward order, starting from the head of the list. This macro also tracks the index (idx) of each element during iteration. var will contain a copy of the value pointed to by each list node, and idx will be the zero-based index of the current element.

Read More

ListForeachPtrInRange

ListForeachPtrInRange Description This macro performs forward traversal, starting at index start (inclusive) and continuing until index end (exclusive), assuming zero-based indexing. Since linked lists are not indexable, the traversal walks node-by-node and skips nodes before start, then continues while tracking the current index.

Read More

ListForeachReverse

ListForeachReverse Description Iterate over each element var of the given list l in reverse order. The variable var is declared and defined by this macro. Iteration happens in reverse, starting from the tail of the list and continuing through the prev pointers until the head is reached. The variable var will contain a copy of the value pointed to by each list node.

Read More