ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – DoublyLinkedList删除错误

2019-07-29 12:16:08  阅读:130  来源: 互联网

标签:doubly-linked-list c


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

这是错误的地方?

Error 1 error C2027: use of undefined type
‘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溢出用户上放置大量代码.

解决方法:

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

让我们分析: –

while (node->m_next != m_itr.m_node)
            node = node->m_next;

在循环节点之后 – > 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;

标签:doubly-linked-list,c
来源: https://codeday.me/bug/20190729/1571602.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有