-1
void DLinkedList::removeName(const Elem& name) {

 DNode * node = header;

 if (node != NULL) {
     do {
         if(node->elem == name){
             node->prev->next = node->next;  
             node->next->prev = node->prev;
             delete node;
         }
         node = node->next;
     } while (node != header); // Stop when we reach the head again.
 }

}

我将它扔到调试器中,在我大约第三次通过 if(node->elem == name) 之后,它由于我不太确定的原因而崩溃。

4

1 回答 1

0

请注意,如果条件为真,那么您delete node并稍后在 处取消引用它node->next,从而导致未定义的行为,这就是导致您崩溃的原因

     if(node->elem == name){
         node->prev->next = node->next;  
         node->next->prev = node->prev;
         delete node; // delete here
     }
     node = node->next; // dereference invalid pointer
于 2012-12-23T19:04:42.303 回答