2017-10-30 2 views
0

첫 번째로, 나쁜 영어로 죄송합니다.Doubly Linked List 방법의 올바른 구현

이중 연결 목록을 구현하려고하지만 일부 메서드가 제대로 작동하는지 확신 할 수 없습니다. 실제로 clear(), remove(const short DATA) -은 DATA, unique()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; 
} 
+2

먼저 종이에 연결된 목록과 각 작업은 무엇을 했습니까? 링크는 선이며, 노드는 상자입니다. 이것은 한 줄의 코드를 작성하기 전에해야 할 일입니다. 그런 다음 종이로 그린 코드를 번역하십시오. 문제가 발생하면 코드를 디버그하여 종이에 그려 놓은 것과 반대되는 부분을 확인하십시오. – 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