Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++中删除链表中某个值的节点_C++ - Fatal编程技术网

在C++中删除链表中某个值的节点

在C++中删除链表中某个值的节点,c++,C++,我正在用一个网站上的一些练习练习来练习编码,我不知道在这个实现中我做错了什么。有人能告诉我我的代码哪里出错了吗 函数removeElements应该从列表中删除具有给定值的所有元素。我试图通过使用另一个名为removeElement singular的函数来实现这一点,并运行它,直到它无法删除任何内容为止 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next

我正在用一个网站上的一些练习练习来练习编码,我不知道在这个实现中我做错了什么。有人能告诉我我的代码哪里出错了吗

函数removeElements应该从列表中删除具有给定值的所有元素。我试图通过使用另一个名为removeElement singular的函数来实现这一点,并运行它,直到它无法删除任何内容为止

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
    bool removeElement(ListNode* head, int val) {
        if (!head)
            return false;

        ListNode* iterator = head;

        //deal with case where head is value to be deleted
        if (head->val == val) {
            head = head->next;
            delete iterator;
            if (head == NULL)
                delete head;
            return true;
        }

        //head didn't match so iterate through list
        while (iterator->next) {

            if (iterator->val == val) {
                ListNode* temp = iterator->next;
                delete iterator;
                iterator = temp;
                return true;
            }

            iterator = iterator->next;

        }//end while loop

        //case where tail is value
        if (iterator->val == val) {
            delete iterator;
            return true;
        }

        //otherwise return false
        return false;
    }//end function removeElement

    ListNode* removeElements(ListNode* head, int val) {

        //Keep calling removeElement until it returns false.
        while (removeElement(head, val)) {
        }

        return head;
    }
};
removeElement方法存在许多问题。首先,如果它删除了头,调用方就无法知道它的列表指针现在已被删除

操作这样的列表的函数应该采用这样的指针引用。。。boolRemoveElementListNode*&head,int-val。通过这种方式,如果函数更改head的值,则调用者将返回新值

接下来,当在这样的单链表上进行迭代时,您应该始终保持指向上一个元素的指针

ListNode* current{head}, prev{nullptr};
while (current) {
    // do something interesting
    prev = current;
    current = current->next;
}
在您的情况下,您希望删除与给定条件匹配的节点

size_t removeElements(ListNode*& head, int val) {
    size_t removed{0};
    ListNode* current{ head }, prev{ nullptr };
    while (current) {
        // Use a nested while loop to check for the condition.  
        // This simplifies the conditions for the outer loop.
        while (current->val == val) {
            ++removed;
            ListNode* oldCurrent = current;
            current = current->next;
            // If this is the head element, update the head value for the caller
            if (oldCurrent == head) {
                head = current;
            }
            // Update the previous element in the list, if any
            if (prev) {
                prev->next = current;
            }
            // delete the removed element
            delete oldCurrent;
        }
        prev = current;
        current = current->next;
    }
    // return the number of element removed.
    return removed;
}

从linkedlist中删除内容的方法如下:

iter->prev->next = iter->next;
iter->next->prev = iter->prev;
delete iter;
但这需要一个双链接列表,这意味着每个元素都指向上一个元素。 如果您不想或不允许添加此内容,可以执行以下操作:

if(iter->next && iter->next->val == val ){
   node* deleteMe = iter->next;
   iter->next = iter->next->next;
   delete deleteMe;
}

删除非头节点时,永远不会更新上一个节点的下一个指针。另外,如果head==NULL,则删除head;如果要完成,它基本上是不可操作的。为什么你要注释掉代码,说明它不会引入编译器错误???@CristiFati ListNode类型可能在别处定义,在这里注释掉只是为了提醒类的布局。嘿,自从我们聊天以来,已经提供了一个解决方案。您的单链接示例永远无法删除头部。