双向链表删除错误

3
我正在制作一个双向链表,我的错误与删除方法有关。我无法理解这个错误。有谁知道吗?
以下是错误的位置:
错误1 error C2027:使用未定义类型 'DoublyListNode' c:\users\conor\documents\college\c++\projects\repeat - doublylinkedlist\repeat - doublylinkedlist\doublylinkedlist.h 230 1 Repeat - DoublyLinkedList
// -------------------------------------------------------------------------------------------------------
//  Name:           Remove
//  Description:    Removes the node that the iterator points to, moves iterator forward to the next node.
//  Arguments:      p_iterator: The iterator to remove
//                  isForward: Tells which direction the iterator was going through the list
//  Return Value:   None.
// -------------------------------------------------------------------------------------------------------
void Remove(DoublyListIterator<Datatype>& m_itr)
{
    DoublyListNode<Datatype>* node = m_head;
    // if the iteratordoesn’t belong to this list, do nothing.
    if (m_itr.m_list != this)
        return;
    // if node is invalid, do nothing.
    if (m_itr.m_node == 0)
        return;
    if (m_itr.m_node == m_head)
    {
        // move the iteratorforward and delete the head.
        m_itr.Forth();
        RemoveHead();
        m_size--;
    }
    else
    {
        // scan forward through the list until you find
        // the node prior to the node you want to remove
        while (node->m_next != m_itr.m_node)
            node = node->m_next;
        // move the iterator forward.
        m_itr.Forth();
        // if the node you are deleting is the tail,
        // update the tail node.
        if (node->m_next == m_tail)
        {
            m_tail = node;
        }
        // delete the node.
        delete node->m_next;
        // re-link the list.
        node->m_next = m_itr.m_node;
        m_size--;
    }
}

如果需要更多的代码,请询问。我不想在Stack overflow用户中放置大量的代码。

你真的希望在不告诉我们错误信息的情况下得到任何帮助吗?现在我们不需要更多的代码,我们需要一个合适的标题和问题。 - stefan
抱歉。我复制粘贴标题时犯了错误,但我已经修改了代码。能否请您重新考虑给我的负评? - Pendo826
现在好多了。我相信你能自己解决这个问题。首先看一下报错所指的那一行代码。它是哪一行? - stefan
这是关于编程的内容:while (node->m_next != m_itr.m_node)。它与while有关,正在寻找数据类型? - Pendo826
当正确的包含文件缺失或在声明中DoublyListNode拼写稍有不同时,我遇到了这个问题。 - MartyE
显示剩余6条评论
2个回答

4
问题出在DoublyListNode类名的一个错字,写成了DLNode。这就导致了上述讨论中的错误。

3
你正在检查尾节点,但没有检查头节点和尾节点之间的节点。你在将节点链接到下一个成员之前就删除了该节点,从而打破了链表。
让我们分析一下:
while (node->m_next != m_itr.m_node)
            node = node->m_next;

循环结束后,node->m_next指向m_itr.m_node

    delete node->m_next;
    // re-link the list.
    node->m_next = m_itr.m_node;

您正在分配一个已删除的节点!!!
更改代码如下:-
node->m_next = m_itr.m_node;
delete m_itr;

1
这更像是一个编译错误 DoublyListNode<Datatype>*,你需要检查一下你的代码。我只是指出了逻辑错误。 - perilbrain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接