Search code examples
clinked-listreversesingly-linked-listfunction-definition

The list is not filled in


I wrote a method that reverse the list, but as a result, the list remains empty. Help us understand what the problem is.

Method for reverse the list:

void reverseList(pLIST pL){
    pNODE pN = pL->top;
    pLIST pLReverse = createList();

    while(pN){
        pNODE pNew = malloc(sizeof(NODE));
        pNew->value = pN->value;
        if(!pLReverse->top){
            pNew->next = NULL;
            pLReverse->top = pNew;
        }
        else{
            pNew->next = pLReverse->top;
            pLReverse->top = pNew;              
        }

        pN = pN->next;
    }

    showList(pLReverse);
}

The structure of the list:

typedef struct Node{
    int value;
    struct Node * next;
} NODE, *pNODE;

typedef struct List{
    int len;
    pNODE top;
} LIST, *pLIST;

Method for printing a list:

void showList(pLIST pL){
    if(isEmpty(pL)) printf("Empty\n");
    else{
        pNODE temp = pL->top;
        printf("Length: %d\n", pL->len);
        while(temp){
            printf("Pointer: %p\tValue: %d\tNext pointer: %p\n", temp, temp->value, temp->next);
            temp = temp->next;
        }
    }
}

Solution

  • For starters it is a bad idea to introduce aliases for pointers like this

    typedef struct List{
        int len;
        pNODE top;
    } LIST, *pLIST;
    

    Using such an alias you for example can not declare a pointer to constant list because this declaration

    const pLIST list;
    

    does not mean the same as

    const struct List *list;
    

    Instead it means

    struct List * const list;
    

    That is not what is required.

    Taking into account this declaration

    pLIST pLReverse = createList();
    

    it seems that you are allocating lists dynamically. There is no need to do so. Lists can be declared as objects with the automatic storage duration.

    The function reverseList should reverse the passed to it list itself not create a new list within the function. Moreover you have a memory leak because the created list pointed to by the pointer pLReverse is not freed.

    Here is a demonstrative program that shows how the function reverseList can be defined.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node
    {
        int value;
        struct Node *next;
    } Node;
    
    typedef struct List
    {
        size_t len;
        Node *head;
    } List;
    
    void init( List *list )
    {
        list->len = 0;
        list->head = NULL;
    }
    
    int pushFront( List *list, int value )
    {
        Node *new_node = malloc( sizeof( Node ) );
    
        int success = new_node != NULL;
    
        if ( success )
        {
            new_node->value = value;
            new_node->next = list->head;
            list->head = new_node;
            ++list->len;
        }
    
        return success;
    }
    
    void showList( const List *list )
    {
        for ( Node *current = list->head; current != NULL; current = current->next )
        {
            printf( "%d -> ", current->value );
        }
    
        puts( "null" );
    }
    
    void reverseList( List *list )
    {
        Node *current = list->head;
        list->head = NULL;
    
        while (  current != NULL )
        {
            Node *new_node = current;
            current = current->next;
            new_node->next = list->head;
            list->head = new_node;
        }
    }
    
    void freeList( List *list )
    {
        while ( list->head != NULL )
        {
            Node *tmp = list->head;
            list->head = list->head->next;
            free( tmp ); 
        }
    }
    
    int main(void) 
    {
        List list;
        init( &list );
    
        const int N = 10;
    
        for ( int i = 0; i < N; i++ )
        {
            pushFront( &list, i );
        }
    
        showList( &list );
    
        reverseList( &list );
    
        showList( &list );
    
        freeList( &list );
    
        return 0;
    }
    

    The program output is

    9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> null
    0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null