DoublyLinkedList删除错误

时间:2012-08-25 19:24:23

标签: c++ doubly-linked-list

我正在制作一个doublelyLinkedList。该错误与我的Remove方法有关。我无法弄清楚这一点。有人知道吗?

这是错误的位置?

  

错误1错误C2027:使用未定义类型   'DoublyListNode'c:\ users \ conor \ documents \ _cancel \ c ++ \ projects \ repeat    - doublylinkedlist \ repeat - doublylinkedlist \ doublylinkedlist.h 230 1重复 - 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溢出用户上放置大量代码。

2 个答案:

答案 0 :(得分:4)

问题是类DoublyListNode的拼写错误。该类名为DLNode。所以这给出了上面讨论的错误。

答案 1 :(得分:3)

您正在检查尾部节点,但不检查头部和尾部之间的节点。您甚至在将节点链接到下一个成员之前删除该节点,从而打破链。

让我们分析: -

while (node->m_next != m_itr.m_node)
            node = node->m_next;
在循环node->m_nextm_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;