2017-10-30 17 views
0

まず、私の悪い英語を申し訳ありません。二重リンクリストメソッドの正しい実装

二重リンクリストを実装しようとしましたが、いくつかのメソッドが正しく機能するかどうかはわかりません。実際、DATAunique()、およびreverse()に等しいすべての要素を削除するclear()remove(const short DATA) - が正しく機能しません。 良いと思われる本、ビデオ、記事は見つかりませんでした。彼らはうまく動作するはずですように私はあなたの方法を修正してきた私は、私の方法でそれらを実装することができますが、(存在する場合)私は正式な方法に固執し、より多くの経験を積んだ人のソリューションは、鉱山

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; 
} 
+2

最初に紙にリンクリストを描きましたか?それぞれの操作は何ですか?リンクは線であり、ノードはボックスである。 1行のコードを書く前にまずやるべきことです。次に、紙の上に描いたコードに翻訳します。何か問題が生じた場合は、コードをデバッグして、紙に書いたものとどこが違うかを確認してください。 – PaulMcKenzie

+1

あなたが投稿している場合は、「正しく動作しません」_、あなたはまた、理由を投稿する必要があります。何が起きたはずですか?代わりに何が起こったのですか? –

+0

@PaulMcKenzie私は何も描いていませんでしたが、今からです。アドバイスありがとう – 0xbaadf00d

答えて

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 
} 

先頭から最後のノードまで反転することもできます。今すぐあなたは紙の中にリストを書き留めて、あなたがそれをやる方法とそれを行うために必要なポインタの数を考えてください。幸運:-)

+0

'DLList :: unique()'メソッドの 'if'ブランチで' temp'ポインタを間接参照しようとすると、コードがセグメンテーションフォルトを引き起こします。とにかく、私はメンバー変数としてテールノードを格納しているので、 'DLList :: reverse()'メソッドで最初の 'while'ループは必要ありません。 – 0xbaadf00d

+0

今すぐお試しください。チュートリアルはこちら –

+0

https://pastebin.com/DXunz58Q – sp2danny

関連する問題