ListForeachPtrReverseIdx

Table of Contents

ListForeachPtrReverseIdx

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 pointer to the value during iteration.
idxoutVariable that will track the index from the head.

Usage example (Cross-references)

    if (list->length > 0) {
    int sum = 0;
    ListForeachPtrReverseIdx(list, item_ptr, idx) {
    sum += *item_ptr + (int)idx;
    }

Share :

Related Posts

ListForeachReverseIdx

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).

Read More

ListForeachPtrReverseInRange

ListForeachPtrReverseInRange Description Iterate over each element var (as a pointer) of the given list l in reverse, limited to index range [start, end) relative to the tail of the list. Index 0 corresponds to the tail, 1 to the previous node, and so on. The variable var is declared and defined by this macro as a pointer to the list’s data type. Since linked lists do not support indexing, this macro counts nodes from the tail and includes only those where the relative reverse index lies in [start, end).

Read More

ListForeachPtrReverse

ListForeachPtrReverse Description Iterate over each element var (as a pointer) of the given list l in reverse order. The variable var is declared and defined by this macro as a pointer to the list’s data type. 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 point to the data associated with each list node.

Read More