简体   繁体   English

使用函数释放带有双指针的链表

[英]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;" 我看到的问题是“ * 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*. 这将释放NULL,而不是您的节点*。

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 . 应该是while (*head != NULL)(*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. 第一个是逻辑错误,因为head将始终为非NULL,第二个是语法错误,因为您需要在访问下一个指针之前取消引用head指针。

This will work. 这将起作用。 You just set next of head to null and freed head. 您只需将head的下一个设置为null并释放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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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