正确实现Doubly Linked List方法

时间:2017-10-30 17:40:08

标签: c++ data-structures implementation doubly-linked-list

首先,抱歉我的英语不好。

我尝试实现双向链接列表,但我不确定它的某些方法是否正常工作。事实上,clear()remove(const short DATA) - 删除所有比较等于DATAunique()reverse()的元素都无法正常工作。 我还没有找到任何好的书,视频或文章。我可以按照自己的方式实施它们,但我坚持正式的方式(如果有)并且更有经验的人的解决方案会比我的更有效

void DLList::clear()
{
    Node *pCurr = mHead;
    while (pCurr != nullptr)
    {
        mHead = pCurr->mNext;
        delete pCurr;
        pCurr = mHead;
    }
}

void DLList::remove(const short DATA)
{
    Node *pCurr = mHead;
    while (pCurr != nullptr)
    {
        if (pCurr->mData == DATA)
        {
            if (pCurr == mHead)
            {
                mHead = pCurr->mNext;
                delete pCurr;
                pCurr = mHead;
            }
            else
            {
                Node *pPrev = pCurr->mPrev;
                pPrev->mNext = pCurr->mNext;
                delete pCurr;
                pCurr = pPrev->mNext;
            }
        }
        else
            pCurr = pCurr->mNext;
    }
}

void DLList::unique()
{
    Node *pCurr = mHead;
    while (pCurr != nullptr)
    {
        Node *pNextDistinct = pCurr->mNext;
        while (pNextDistinct != nullptr && pNextDistinct->mData == pCurr->mData)
        {
            pNextDistinct = pNextDistinct->mNext;
            delete pCurr->mNext;
            pCurr->mNext = pNextDistinct;
            pNextDistinct->mPrev = pCurr;
        }
        pCurr = pNextDistinct;
    }
}

void DLList::reverse()
{
    Node *pPrev = nullptr;
    Node *pCurr = mHead;
    while (pCurr != nullptr)
    {
        pCurr->mPrev = pCurr->mNext;
        pCurr->mNext = pPrev;
        pPrev = pCurr;
        pCurr = pCurr->mPrev;
    }
    mHead = pPrev;
}

1 个答案:

答案 0 :(得分:0)

我已经纠正了你的方法,以便他们能够很好地工作。请比较您的错误,并在代码中阅读我的推荐。在对数据结构进行编码时,请始终使用纸和笔,然后首先记下树或列表结构,这将有助于您先设计算法然后再编码。如果您需要更多帮助,请与我们联系。

//In the below method you are deleting unique nodes
void DLList::unique()
{
    Node *pCurr = mHead;
    Node *temp = NULL;
    int data = 0;
    while (pCurr != NULL)
    {
       data = pCurr->mData;
       temp = pCurr;
       while(temp != NULL)
       {
           temp = temp->mNext;
           if(temp->mData == data)
           {
              //delete the duplicate node
              temp->mPrev->mNext = temp->mNext;
              if(temp->mNext != NULL)
                  temp->mNext->mPrev = temp->mPrev;
              delete(temp);
           }
       }
      pCurr = pCurr->mNext; 
    }
}


void DLList::reverse()
{
    Node *temp = NULL, *q = NULL;
    while (temp->mNext != NULL)
      temp = temp->mNext;
    //now temp is pointing to the last node of the list
    //Assume that mHead->mPrev == null, because I dont know whether it is null or address of Head itself
    //if Head node mPrev is holding the address of head then you must store the address of head in a pointer
    //to check whether you reach head node or not
    //as I assume it is null so I don't need a temporary pointer here
    mHead = temp; //now head is pointing to the last node
    q = temp->mPrev;//pointer q is pointing to the previous node of the last node
    temp->mPrev = NULL;//as last node is now first node make it's previous pointer as NULL
    while(q!=NULL)// now traverse toward the old head node who's prev pointer is set as NULL
    {
       temp->mNext = q;
       q->mPrev = temp;
       temp = q; //move temp to the old previous node
       q = q->mPrev;// Move q to the previous node
    }
    //Now temp is pointing to the old head node
    temp->mNext = NULL;//Your list is reversed finally
}

也可以从头到尾完成反转。现在,您只需在纸上写下列表,然后考虑如何做到这一点以及需要多少指针。祝你好运: - )