My go at creating a function to remove a specific node from a linked list:
void deleteSpecific(DataType N, Node *&H, Node *&T)
{
if (H == nullptr) // 1. Check Whether List Is Empty (head == NULL)
{ // If It's Empty Then, Display The Following And Terminate The Function.
std::cout << std::endl << std::endl;
std::cout << "\t" << "-> LL| ERR: The List Is Empty. Deletion Not Possible.";
std::cout << std::endl << std::endl;
return;
}
Node *TMP1 = H, *TMP2; // If It's Not Empty Then, Define Two Node Pointers Temp1 And Temp2 And Put Head In Temp1 Only.
while (TMP1->data != N) // Keep Moving Temp1 Until it reaches to exact node to delete
{
if (TMP1->next == nullptr) // If Node is not found in list then display the message and exit the loop and function.
{
std::cout << std::endl << std::endl;
std::cout << "\t" << "-> LL| ERR: Given Node Not Found. Deletion Not Possible.";
std::cout << std::endl << std::endl;
return;
}
TMP2 = TMP1;
TMP1 = TMP1->next;
}
if (H->next == nullptr)
{
H = nullptr;
delete TMP1;
}
else if (TMP1 == H)
{
H = H->next;
delete TMP1;
}
else if (TMP1->next == nullptr)
{
TMP2->next = nullptr;
T = TMP2;
delete TMP1;
}
else
{
TMP2->next = TMP1->next;
delete TMP1;
}
}
Is The logic of the code correct? I've tried it with multiple inputs and it works, however are there any special cases that you can notice that'll not work with this?