从双向链表中删除节点

时间:2013-11-25 01:45:18

标签: c++ c++11

我已经在这里查看了其他主题,但是无法使用它们来解决我的问题。


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

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;

};

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

删除节点的功能:http://pastebin.com/HAbNRM5W

^这是我需要帮助的代码,重新输入

太多了

我的导师告诉我,问题的代码是第56行,其中包括:

tmp->prev = prev;

我正在尝试将前一个节点的链接设置为正确的节点。我试图使用类似if/else循环的情况是当前节点是否是列表中的最后一项。如果它是最后一项(又名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

4 个答案:

答案 0 :(得分:1)

对于您的代码,我建议几件事。隔离代码以查找具有您要查找的名称的节点。 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;

答案 1 :(得分:1)

NULL应替换为nullptr

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

这是一项任务,你想要:

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

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

prev->next = curr->next;

由于prev是nullptr,因此无效。

答案 2 :(得分:0)

应该是这样的:

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

答案 3 :(得分:0)

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

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;
    }
}