问一下关于链表类的RemoveAt函数如果用RemoveAt删除了一个节点,(不是头、尾,是中间的其中一个),那样遍历链表时能不能完全遍历?假设已有一个链表m_PtrList,里面有若干条数据,首先我删除了其中几条记录,(中间的),然后遍历POSITION pos=m_PtrList.GetHeadPosition();
for(int i=0;i<m_PtrList.GetCount();i++)
{
    m_PtrList.GetAt(pos);
    m_PtrList.GetPrev(pos);
}
这样会有什么问题吗?

解决方案 »

  1.   

    MSDN上的,删除不能用GetCount()吧
    CObList list;
    POSITION pos1, pos2;
    CObject* pa;list.AddHead(new CAge(21));
    list.AddHead(new CAge(40));
    list.AddHead(new CAge(65)); // List now contains (65 40, 21).
    for (pos1 = list.GetHeadPosition(); (pos2 = pos1) != NULL;)
    {
        if (*(CAge*) list.GetNext(pos1) == CAge(40))
        {
            pa = list.GetAt(pos2); // Save the old pointer for
                                   //deletion.
            list.RemoveAt(pos2);
            delete pa; // Deletion avoids memory leak.
        }
    }
    #ifdef _DEBUG
       afxDump.SetDepth(1);
       afxDump << _T("RemoveAt example: ") << &list << _T("\n");
    #endif      
      

  2.   

    哦 GetCount 只是用来计数的,稍后我修改一下
    我的问题是会不会出现链表断开,个别没有链接到的情况
      

  3.   

    removeat删除一个节点,后面节点的索引都会减1