Deleting a node from linked list in C

前端 未结 6 1978
醉酒成梦
醉酒成梦 2020-12-07 04:05

My problem is deleting a node from linked list.

I have two structs :

typedef struct inner_list 
{
 int count;
 char word[100];
 inner_list*next;
}          


        
6条回答
  •  悲&欢浪女
    2020-12-07 04:36

    try this (only for the outer list, it wont release the inner one):

    void delnode(outer_list *head,char num[100]) 
    { 
        outer_list *temp, *m. *helper; 
        temp=head; 
        while(temp!=NULL) 
        { 
            if(strcmp(temp->word,num)==0) 
            { 
                if(temp==head) 
                { 
                    head=temp->next; 
                    free(temp); 
                    return; 
                } 
                else 
                { 
                    m = temp;
                    temp = temp->next;
                    helper->next = temp; //link the previous item
                    free(m); 
                    return; 
                } 
            }
            else 
            { 
                helper = temp;
                temp= temp->next; 
            } 
    
        } 
        printf(" ELEMENT %s NOT FOUND ", num); 
    } 
    

提交回复
热议问题