删除链表中的节点

时间:2014-01-23 06:05:49

标签: c linked-list

大多数代码都有效,但是当我尝试删除列表的最后一个元素并打印出来时,我在其中看到了一些垃圾数据,我在这里做错了什么?有人能指出我的错误吗?

void DeleteClient2(struct client *temp,struct client **head)
{   struct client *prev=*head;
    struct client *current = *head;
    struct item *currentitem = (*head)->item_data,*save;
    if(temp== *head)
    {
        while(currentitem != NULL)
        {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        free(temp);
        temp->item_data = NULL;
        (*head) = (*head)->next;
    }
    else
    if(temp->next == NULL)
    {
        while(currentitem != NULL)
        {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        temp->item_data = NULL;
        free(temp);
    }
    else
    if(temp != *head && temp->next != NULL)
    {
        while(prev->next != temp)
        {
            prev=prev->next;
        }
        prev->next = temp->next;
        while(currentitem != NULL)
        {
            save = currentitem;
            currentitem = currentitem ->next;
            free(save);
        }
        temp->item_data = NULL;
        free(temp);
        temp=temp->next;
    }
}

3 个答案:

答案 0 :(得分:2)

代码:

    free(temp);
    temp=temp->next;  // temp->next is invalid

是未定义的行为,一旦释放了一个无法访问它的节点,这样做是非法的。

答案 1 :(得分:0)

在这里检查* head == temp,如果是,你可以释放温度,即头部因为它们相等,你只是验证它,而不是你说(* head)=(* head) - > next;最可能将*设置为垃圾

if(temp== *head)
{
    while(currentitem != NULL)
    {
        save = currentitem;
        currentitem = currentitem ->next;
        free(save);
    }
    free(temp);
    temp->item_data = NULL;
    (*head) = (*head)->next;
}

同样的混乱在这里

    temp->item_data = NULL;
    free(temp);
    temp=temp->next;

答案 2 :(得分:0)

void DeleteClient2(struct client *temp,struct client **head)
{   struct client *prev=*head;
    struct client *current = *head;
    struct item *currentitem = (*head)->item_data,*save;
    if(temp== *head) \\ if head is the node to be deleted
    {
        while(currentitem != NULL)
        {
            save = currentitem;
           *head = currentitem->next;
          \\  currentitem = currentitem ->next;
            free(save);
        }

    }
    else
    if(temp->next == NULL) \\ if the node to be deleted is last node then
    {
    while(currentitem->next != NULL)
        {
           *prev = currentitem;
            currentitem = currentitem ->next;

        }
        prev->next = NULL;
        free(currentitem);
}
else
if(temp != *head && temp->next != NULL)\\ node to be deleted is between head and last node
{while(prev->next != temp)
{   currentitem = *prev;
    prev=prev->next;
}
currentitem->next = temp->next;
free(temp);
\\while(currentitem != NULL)
    \\    {
    \\        save = currentitem;
    \\        currentitem = currentitem ->next;
    \\\        free(save);
    \\    }
      \\  temp->item_data = NULL;
     \\   free(temp);
     \\   temp=temp->next;
}
}