简体   繁体   English

删除链表中的节点

[英]Removing node in a linked list

I just started getting into nodes and simple linked lists. 我刚刚开始了解节点和简单的链接列表。 While I find linked lists confusing I still start to understand them more and can code them following different tutorials. 当我发现链接列表令人困惑时,我仍然开始对它们有所了解,并且可以按照不同的教程进行编码。

But to remove nodes I tried a piece of code that didn't quite work and found another one that did. 但是为了删除节点,我尝试了一段不太奏效的代码,然后又找到了一个。 Except I don't quite get why one works and the other doesn't. 除了我不太明白为什么一个有效而另一个无效。

Here is what doesn't work: 这是行不通的:

void remove_character(Character **head)
{
Character *current=*head,
    *temp=malloc(sizeof(Character));//temporary node to 
     //remove node
char *name=malloc(100);

if(current!=NULL)//if there is at least one node
{
    printf("Enter name: ");
    scanf("%s", name);
}

if(!strcmp(name, (*head)->name))//removes head and 
//makes next node head
{
    temp=(*head);
    (*head)=(*head)->next;
    free(temp->name);
    free(temp);
    current=*head;
}

while(current!=NULL)
{
       if(!strcmp(name, current->name))
    {
        temp=current;//assign node to be removed
        current=current->next;//move to next node
        free(temp->name);
        free(temp);//free removed node
    }
    current=current->next;
}
}

But this works: 但这有效:

void remove_character(Character **head)
{
Character *current=*head, 
  *temp=malloc(sizeof(Character));
char *name=malloc(100);

if(current!=NULL)
{
    printf("Enter name: ");
    scanf("%s", name);
}

if(!strcmp(name, (*head)->name))
{
    temp=(*head);
    (*head)=(*head)->next;
    free(temp->name);
    free(temp);
    current=*head;
}

while(current!=NULL)
{
    if(current->next!=NULL)
    {
        if(!strcmp(name, current->next->name))
        {//if next node is node to be removed
            temp=current->next;
            current->next=current->next->next;
                            //moves to next node
            free(temp->name);
            free(temp);
        }
    }
    current=current->next;
}
}

Basically the second one anticipates one node and links one node further but why should this one work and not the other (which prints segmentation fault right where the removed node was... Nodes seem so easy but are so complicated to manage. Especially when you are bad at imagining how it works. 基本上,第二个节点预期一个节点并进一步链接一个节点,但是为什么这个节点应该工作而不是其他节点(这会在被删除的节点所在的位置显示分割错误...节点看起来很简单,但管理起来却很复杂,尤其是当您很难想象它是如何工作的。

In your first method you are not proactive. 在第一种方法中,您并不主动。

That is you check for the string after visiting the particular node and once you found the node which is to be deleted you just free it and move further without fixing the list . 也就是说,您在访问特定node后检查string ,一旦找到要删除的node ,您就可以free它并继续移动而不固定list

Adding one more pointer to point the previous node should fix this issue. 添加一个以上的pointer指向上一个node应该可以解决此问题。

        if(!strcmp(name, (*head)->name))
        {
            /* Code to delete in the head node */ 

            temp=(*head);
            (*head)=(*head)->next;
            free(temp->name);
            free(temp);
            current=*head;
        }
        else 
        {

            /* Code to delete in the rest of the list*/ 
            character *prev=*head;
            while(current!=NULL)
            {
                if(!strcmp(name, current->name))
                {
                    prev->next = current->next;//move to next node
                    free(current->name);
                    free(current);//free removed node
                    break;
                }
                prev = current;
                current=current->next;
            }
        }

In your second method you are proactive. 在第二种方法中,您很主动。

That is you check for the string before visiting the particular node and once you found out that next node which is to be deleted then you free it and move further after fixing the list . 也就是说,您在访问特定node之前检查string ,一旦找到要删除的下一个node ,就可以free它,并在修复list之后进一步移动。

if(!strcmp(name, current->next->name))   // Found out next node to be deleted
{
     temp=current->next;                 // Get the next node
     current->next=current->next->next;  // Adjust the list by skipping the next node

     free(temp->name);                   // Free the node.
     free(temp);
}

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

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