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와 같지 않습니다.
현재가 업데이트되지 않기 때문에 조건을 current->value != removeValue
떠나지 않을 것이기 때문에 루프의 첫 번째 반복에서 무한 루프가있는 것으로 보입니다 if (current == head)
. 첫 번째 조건을에서 if (current == head)
로 변경 if (current == head && current->value == removeValue)
하면 이제 current->value != removeValue
건너 뛰고 있다고 말한 조건에 올바르게 도달해야합니다 .
프로그램이 건너 뛰는 단계에 대한 자세한 정보 없이는 그 이상의 추가 오류가 표시되지 않습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다