从双链接列表中删除节点

Removing a node from a doubly linked list

本文关键字:删除 节点 列表 链接      更新时间:2023-10-16

我在这里查看了关于该主题的其他线程,但无法使用它们来解决我的问题。


这是链接列表中节点的主要类定义:

class node {  
public:
    // default constructor
    node() {name = ""; prev = NULL; next = NULL;};
    // default overloaded
    node(string s) {name = s; prev = NULL; next = NULL;};
    // item in the list
    string name; 
    // links to prev and next node in the list
    node * next, * prev;
};

上面是节点类定义,它用于生成链表的另一个类。linkedlist代码是给我们的,我们必须修改它,所以我知道它是有效的。我已经完成并测试了在双链接列表中添加新节点的工作,现在我正在从同一个双链接列表删除节点。

删除节点的函数:http://pastebin.com/HAbNRM5W

^这是我需要帮助的代码,有太多的内容需要重新键入


我的老师告诉我,问题出在第56行,上面写着:

tmp->prev = prev;

我正在尝试将到上一个节点的链接设置为正确的链接。我尝试使用类似的CCD_ 1循环的情况是当前节点是否是列表中的最后一项。如果它是最后一项(也称为curr->next = NULL),则不要使用curr->next设置链接并停止循环迭代。

任何帮助/想法/建议/反馈都将不胜感激!

void linkedList::remove(string s) 
{
        bool found = false;
        node * curr = getTop(), * prev = NULL;
        node * tmp = new node();
        while(curr != NULL) 
        {
                 // match found, delete
                 if(curr->name == s) 
                 {
                        found = true;
                        // found at top
                        if(prev == NULL) 
                        {
                            node * temp = getTop();
                            setTop(curr->next);
                            getTop()->prev = NULL;
                            delete(temp);
                        } // end if
                        else 
                        {
                            // determine if last item in the list
                            if (curr->next = NULL) 
                            {
                                // prev node points to next node
                                prev->next = curr->next;
                                // delete the current node
                                delete(curr);
                            } // end if
                            // if not last item in list, proceed as normal
                            else 
                            {
                                // prev node points to next node
                                prev->next = curr->next;
                                // set the next node to its own name
                                tmp = prev->next;
                                // set prev-link of next node to the previous node (aka node before deleted)
                                tmp->prev = prev;
                                // delete the current node
                                delete(curr);
                            } // end else
                        } // end else
                    } // end if
                // not found, advance pointers
                if(!found) 
                {
                    prev = curr;
                    curr = curr->next;
                } // end if
                // found, exit loop
                else curr = NULL;
        } // end while
        if(found)
            cout << "Deleted " << s << endl;
        else 
            cout << s << " Not Found "<< endl;
} // end remove

NULL应替换为nullptr

if (curr->next = NULL) { ...

这是一项任务,你想要:

if (curr->next == nullptr) { ...

在第47行,我想你会说:如果prev==nullptr和next不是nullptr,但你使用

prev->next = curr->next;

这不起作用,因为prev是nullptr。

对于您的代码,我提出了几点建议。隔离代码以查找具有您要查找的名称的节点。remove方法应该只删除一个双链接节点,前提是它有一个。

我知道您的remove方法接受一个字符串参数,但将其传递给另一个函数,并让该函数返回您要查找的节点。

它应该看起来像这样:

Node *cur = find("abcd");
Node *prev = cur->prev;
prev->next = cur->next;
Node *n = cur->next;
n->next = cur->prev;
cur->next = NULL; //or nullptr
cur->prev = NULL; //or nullptr
delete cur;

应该看起来像:

prev->next = curr->next;
prev->next->prev = prev;
delete (curr);

我迷失在你所有不同的条件中。你所需要做的就是:

void linkedList::remove(const std::string& s)
{
    node* current = getTop(); // get head node
    while (current != nullptr) // find the item you are trying to remove
    {
        if (current->name == s)
        {
            break; // when you find it, break out of the loop
        }
    }
    if (current != nullptr) // if found, this will be non-null
    {
        if (current->prev) // if this is not the head node
        {
            current->prev->next = current->next;
        }
        else
        {
            // update head node
        }
        if (current->next) // if this is not the tail node
        {
            current->next->prev = current->prev;
        }
        else
        {
            // update tail  node
        }
        // at this point, current is completely disconnected from the list
        delete current;
    }
}