2013-12-13 40 views
1
int SDI::LinkedList::removeValue(int removeValue) 
{ 
SDI::Node* current = head; 
SDI::Node* prev = head; 
    while (current != nullptr) 
    { 
     if (current == head) 
     { 
      if (current->value == removeValue) 
      { 
       current = current->next; // track the next value 
       delete head; 
       head = current-> next; 
       return 1; 
      }  

     } 
     else 
     { 
      if (current->value != removeValue) 
      { 
       prev = current; /// don’t lose track of the previous value, saves working 1 ahead 
       current = current-> next; // let the loop deal with it 

      } 
     else 
     { 
      prev->next = current->next; 
      delete current; 
     } 
      if (current->next != nullptr) 
      { 
       if (current->next->value == removeValue) 
       { 
       SDI::Node* temp = current->next; 
       current->next = current->next->next; 
       delete temp; 
       return 1; 
       } 
      } 
     } 
    } 
return 0; 
} 

一切似乎很好地工作,直到它到达线路:从自定义链接列表中删除值

"if (current->value != removeValue)" 

这似乎只是直接跳过去,但电流 - >值不等于如观察者中所示的removeValue。

+0

错误是什么? – Matt

+0

您是否在发布模式下编译? –

回答

1

看起来你有一个无限循环,如果current->value != removeValue在循环的第一次迭代,因为你永远不会离开if (current == head)条件,因为目前不会更新。如果您将第一个条件从if (current == head)更改为if (current == head && current->value == removeValue),它现在应该正确达到您所说的被跳过的current->value != removeValue条件。

我没有看到任何额外的错误,没有更多的细节,你的程序似乎跳过的步骤。

+0

完美,谢谢 – Broak