简体   繁体   中英

Using a function to free a linked list with double pointer

I am having a tough time deleting all members in a linked in a single function. If I break it up like you see below, it works fine, but this seems wildly inefficient and want to figure out the correct way to do this. in order to free all nodes I need to have function to first free all nodes other then the head, then have a function free the head link. this seems like it would be easy to do but I am having trouble.

Thanks for the help!

int main() {

    struct node *head = NULL;
    createList(&head);

    //do stuff with list

    freeListMembers(head);
    freeListHead(&head);

    return 0;
}

int createList(struct node **head) {
    //create list
    return 0;
}

void freeListMembers(struct node *head){
    while(head->next != NULL){
        head->next = NULL;
        free(head->next);  
    }
    return;  
}

void freeListHead(struct node **head) {
    *head = NULL;
    free(*head); 
    return;
}

here is the code that I want to work but does not. the issue I am seeing is a an error for "*head->next;" where it sais "expression must have pointer to struct or union type"

int main() {

    struct node *head = NULL;
    createList(&head);

    //do stuff with list

    freeAllListMembers(&head);

    return 0;
}

int createList(struct node **head) {
    //create list
    return 0;
}

void freeAllListMembers(struct node **head){ 
    while (head != NULL) {
        struct node *temp = *head->next;
        free(*head);
        *head = temp ;
    }
   return;  
}

From your code :

void freeListMembers(struct node *head){
    while(head->next != NULL){
        head->next = NULL;
        free(head->next);  
    }
    return;  
}

This is freeing NULL, not your node*.

Freeing the list is as simple as using a temporary pointer to the next node.

while (head) {
    node* next = head->next;
    free(head);
    head = next;
}

From your edit :

void freeAllListMembers(struct node **head){ 
    while (head != NULL) {
        struct node *temp = *head->next;
        free(*head);
        *head = temp ;
    }
   return;  
}

There are a couple errors with this. It should be while (*head != NULL) and (*head)->next . The first is a logic error, because head will always be non-NULL, and the second is a syntax error, because you need to dereference the head pointer before accessing the next pointer.

This will work. You just set next of head to null and freed head. Now we can not move to second element.So we wont be able to free the nodes.Also check base condition. I hope it helps

void freeListmembers(node *head){
node *temp=head;
if(head==NULL)//Base condition
return;
while(head->next!=NULL){
temp=head;//Moved temp to head. we will move head to next and free the previous node
head=head->next;
free(temp);
}
free(head);
return;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM