2012-08-25 39 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。所以这给了上面讨论的错误。