简体   繁体   English

如何从 c++ 的双向链表中删除特定数字?

[英]How do I delete a specific number from doubly linked list in c++?

I was assigned to delete a number entered by user from a doubly linked list.我被分配从双向链表中删除用户输入的数字。 So my code is successfully finding the number in the linked list and checking whether the linked list is empty or not but it is not able to delete it.所以我的代码成功地找到了链表中的数字并检查链表是否为空,但无法删除它。 It deletes the whole linked list instead of the number itself.它删除整个链表而不是数字本身。 I am unable to find the mistake.我找不到错误。 So kindly help me as I am literally stuck here.所以请帮助我,因为我真的被困在这里。 Here is my code:这是我的代码:

void deleteSpcificValue(int val)
{
    int result = 0;
    Node *curr = head;
    if(head == nullptr)
    {
        curr = head;
        cout << "List Is Empty"<<endl;
    }
    else
    {
        while(curr != nullptr)
        {
            if(curr -> data == val)
            {
               cout << "Value Deleted" << endl;
                result = 1;
                curr = nullptr;
                delete curr;
            }
            curr = curr -> next;
        }
    }
   if(result != 1)
    {
        cout << "The Value To Be Deleted Is Not Found" <<endl;
    }

}

The function does not make sense because it does not update data members next and prev (I assume that you have the name prev or something similar for the pointer that points to the previous node) function 没有意义,因为它不会更新数据成员nextprev (我假设您的名称为prev或指向前一个节点的指针的类似名称)

Also it does not delete the found node because you set it to nullptr它也不会删除找到的节点,因为您将其设置为nullptr

curr = nullptr;
delete curr;

Also as after this code snippet the pointer curr is equal to nullptr then the following statement同样,在此代码片段之后,指针curr等于nullptr然后是以下语句

curr = curr -> next;

invokes undefined behavior.调用未定义的行为。

And if the deleted node is the head node then your function does not update the pointer to the head node.如果删除的节点是head节点,那么您的 function 不会更新指向头节点的指针。

Pay attention to that the function should not issue any message.注意 function 不应发出任何消息。 It is the caller of the function that will decide whether to issue a message. function 的调用者将决定是否发出消息。

The function can be defined the following way function 可以通过以下方式定义

bool deleteSpcificValue( int val )
{
    Node *current = head;

    while ( current != nullptr && current->data != val )
    {
        current = current->next;
    }

    bool success = current != nullptr;

    if ( success )
    {
        if ( current->next != nullptr )
        {
            current->next->prev = current->prev;
        }
        // If the class has a pointer to the tail node
        //   then uncomment the else part  
        /*
        else
        {
            tail = current->prev;
        }
        */

        if ( current->prev != nullptr )
        {
            current->prev->next = current->next;
        }
        else
        {
            head = current->next;
        }

        delete current;
    }

    return success;
}

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

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