Search code examples
clinked-listsingly-linked-listc-stringsfunction-definition

why does my delete search linked list function not working?


i have this problem when im using this function, i want to delete all the node with the given string, but when i run this on my program it deletes all from the first find char ex[], im new to c please guide me

void delSearch(char ex[]){
    int del;
    struct file *temp = head;
    while(temp!=NULL){
        if(strcmp(temp->ex,ex)==0){
            file *temp2 = temp->next;
            file *temp3 = temp;
            temp->next=temp2;
            free(temp3);
            temp3 = NULL;
        }
        else
        {
        temp = temp->next;
        }
    }
}

Solution

  • For starters the local variable del

    int del;
    

    is not used within the function.

    The function does not change the global pointer head provided that it points to a node that stores the given string.

    Also you need to update the data member next of the node that precedes to the node to be deleted.

    The function can look the following way as it is shown below.

    void delSearch( const char ex[] )
    {
        while ( head && strcmp( head->ex, ex ) == 0 )
        {
            struct file *tmp = head;
            head = head->next;
            free( tmp );
        }
        
        if ( head )
        {
            for ( struct file *current = head; current->next != NULL; )
            {
                if ( strcmp( current->next->ex, ex ) == 0 )
                {
                    struct file *tmp = current->next;
                    current->next = current->next->next;
                    free( tmp );
                }
                else
                {
                    current = current->next;
                }
            }
        }
    }   
    

    If you want to know how many nodes were deleted after calling the function then the function can be changed the following way.

    size_t delSearch( const char ex[] )
    {
        size_t total = 0;
        
        while ( head && strcmp( head->ex, ex ) == 0 )
        {
            struct file *tmp = head;
            head = head->next;
            free( tmp );
            ++total;
        }
        
        if ( head )
        {
            for ( struct file *current = head; current->next != NULL; )
            {
                if ( strcmp( current->next->ex, ex ) == 0 )
                {
                    struct file *tmp = current->next;
                    current->next = current->next->next;
                    free( tmp );
                    ++total;
                }
                else
                {
                    current = current->next;
                }
            }
        }
        
        return total;
    }   
    

    An alternative function definition can look the following way

    size_t delSearch( const char ex[] )
    {
        size_t total = 0;
        
        for ( struct file **current = &head; *current != NULL; )
        {
            if ( strcmp( ( *current )->ex, ex ) == 0 )
            {
                struct file *tmp = *current;
                *current = ( *current )->next;
                free( tmp );
                ++total;
            }
            else
            {
                current = &( *current )->next;
            }
        }
        
        return total;
    }