3

私は doublelyLinkedList を作成しています。エラーは、Remove メソッドに関係しています。私はこれを理解することはできません。誰か知っていますか?

エラーの場所はここですか?

エラー 1 エラー C2027: 未定義の型 'DoublyListNode' の使用 c:\users\conor\documents\college\c++\projects\repeat - doublelylinkedlist\repeat - doublelylinkedlist\doublelylinkedlist.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--;
    }
}

もうコードが必要な場合は、尋ねてください。スタック オーバーフロー ユーザーに多くのコードを配置したくありません。

4

2 に答える 2

4

問題は、クラスDoublyListNodeのタイプミスでした。クラスの名前はDLNodeでした。したがって、これにより、上記で説明したエラーが発生しました。

于 2012-08-25T20:39:27.603 に答える
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;
于 2012-08-25T19:39:00.023 に答える