Question
Write a function that deletes a node from a singly-linked list holding integers. The function takes one parameter which is the integer of the node
Write a function that deletes a node from a singly-linked list holding integers. The function takes one parameter which is the integer of the node that's supposed to be deleted.
Possible cases:
The calling object is empty. For this case, print the error message, "Cannot delete from an empty list."
The node to be deleted is the first node in the list.
The node to be deleted is somewhere in the list (could be the last node).
The key is not found; therefore, there is nothing to delete. oFor this case, print the error message, "Item to be deleted is not in the list."
Expected output:
Header file:
Current progress:
void AnyList::deleteNode(int deleteData)
{
if (count = 0) cerr
Node *current = ptrToFirst;
Node *afterCurrent = current->getPtrToNext();
if (current->getData() == deleteData)
{
delete current;
current = afterCurrent;
afterCurrent = afterCurrent->getPtrToNext();
}
else
{
while (afterCurrent->getPtrToNext() != nullptr)
{
if (afterCurrent->getData() == deleteData)
{
current = afterCurrent->getPtrToNext();
delete afterCurrent;
afterCurrent = current->getPtrToNext();
count--;
}
}
}
}
Inserted: 2 3 4 5 6 List is: 6 5 4 3 2 Deleting 100... Item to be deleted is not in the list. Deleting 4... List is now: 6 5 3 2 Deleting 2... List is now: 6 5 3 Deleting 6... List is now: 5 3 Deleting 5... List is now: 3 Deleting 3... List is empty. Deleting 2 from an empty list.. . Cannot delete from an empty list. Press any key to continueStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started