Skip to content

ListReverse

Description

Reverse the order of nodes in the list in place.

Parameters

Name Direction Description
l in,out List to be reversed.

Success

Returns to the caller. The previous head is now the tail and vice versa; every next / prev link has been flipped. The list length is unchanged.

Failure

Function cannot fail.

Usage example (Cross-references)

Usage examples (Cross-references)
            // Advanced operations
            case LIST_INT_REVERSE : {
                ListReverse(list);
                break;
            }
    
    static bool test_list_sort_and_reverse(void) {
        WriteFmt("Testing ListSort and ListReverse\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        bool result = list_matches(GENERIC_LIST(&list), (const int[]) {1, 2, 2, 3, 4}, 5);
    
        ListReverse(&list);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {4, 3, 2, 2, 1}, 5);
    
    static bool test_list_sort_and_reverse_edge_cases(void) {
        WriteFmt("Testing ListSort and ListReverse edge cases\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        ListSort(&empty, compare_ints);
        ListReverse(&empty);
        ListPushBackR(&singleton, 42);
        ListSort(&singleton, compare_ints);
        ListPushBackR(&singleton, 42);
        ListSort(&singleton, compare_ints);
        ListReverse(&singleton);
    
        bool result = (ListLen(&empty) == 0) && (ListHead(&empty) == NULL) && (ListTail(&empty) == NULL);
Last updated on