실제로 노드를 삭제하지 않고 메모리를 확보하는 것처럼 느껴집니다. 나는 포인터를 움직이는 것만으로 링크 된 목록을 인쇄 할 때 목록에서 삭제 한 요소가 인쇄되지 않는다고 생각합니다. 그래서 내 질문은 내가 실제로 노드를 삭제하거나 노드를 삭제하는 것처럼 보이도록 포인터를 다시 정렬하는 것입니다 (본질적으로 링크를 끊지 만 노드를 삭제하지 않음)? 어떤 도움을 주셔서 감사합니다.C++에서 연결된 목록의 노드를 올바르게 삭제하는 방법
void SLL::deleteNode(int target){
Node *current = new Node;
Node *previous = new Node;
for (current = front->next, previous = front; current != NULL; current = current->next, previous=previous->next){
if (previous->data == target && previous == front){
front = previous->next;
delete[] previous;
return;
//This if statement deletes the element if its the front
}
else {
if (previous->data == target && previous->next == NULL){
previous = NULL;
delete[] current;
return;
//This if statement deletes the node if it is the back
}
else if (current->data==target)
{
previous->next = current->next;
delete[] current;
return;
//This if statement deletes a node if it is in the middle
}
}
}
delete[] current;
delete[] previous;
}
왜 'current'와 'previous'를 하나의 인스턴스로 선언 할 때 배열 delete ('delete []') 버전을 사용하고 있습니까? – mathematician1975
'front-> next'로 열거 알고리즘을 시작하는 것은 관련이 있습니다. * 실제로 데이터가 포함되지 않은 사전 할당 된 "헤드"노드를 사용하지 않는다고 알려주십시오. 필요하지 않습니다. 그리고 노드를'new Node [n]'으로 할당하지 않으면 잘못된'delete' 연산자를 사용하게됩니다. – WhozCraig
나는 모른다. 나는 이것을 시도했다. 질문에 대답하십시오. – WombatCombat