2012-08-25 38 views
3

Im製作一個雙向鏈表。錯誤是用我的刪除方法。我無法弄清楚這一點。有人知道嗎?DoublyLinkedList刪除錯誤

這裏是錯誤的地方?

錯誤1個錯誤C2027:使用未定義的類型 'DoublyListNode' C:\用戶\康納爾\文件\學院\ C++ \項目\重複 - doublylinkedlist \重複 - doublylinkedlist \ doublylinkedlist.h 230 1重複 - 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--; 
    } 
} 

如果需要再代碼只問。我不想在堆棧溢出用戶上輸入很多代碼。

+0

你看到的錯誤究竟是什麼?例如:編譯器/鏈接器,不希望的運行時行爲(具體)?還是SEGFAULT? – MartyE

+0

你真的希望得到任何幫助,甚至沒有告訴我們錯誤是什麼?我們現在不需要更多的代碼,我們需要一個合適的標題和一個問題。 – stefan

+0

apoogies。我複製並粘貼了標題。這樣做時我犯了一個錯誤。我編輯了我的代碼。你能否再認爲我的失望。 – Pendo826

回答

3

您正在檢查尾部節點,但不是針對頭部和尾部之間的節點。您正在通過在將節點鏈接到下一個成員之前刪除節點來打破鏈條。

讓我們來分析: -

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

循環後node->m_nextm_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; 
+0

我仍然得到相同的錯誤代碼的變化:( – Pendo826

+1

這是更多的編譯錯誤'DoublyListNode *',你將需要看你的代碼。我justed指出了邏輯錯誤。 – perilbrain

4

的問題是類DoublyListNode的錯字。這個類名爲DLNode。所以這給了上面討論的錯誤。