Write a function that deletes a node from a singly-linked list holding integers.
ID: 3752218 • Letter: W
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 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 << "Cannot delete from an empty list.";
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 continue . . .Explanation / Answer
Program Code
void AnyList::deleteNode(int deleteData)
{
//count = 0 will assign count to be 0 and always return true
//to compare values use comparision operator ==
if (count == 0)
{
cerr << "Cannot delete from an empty list.";
return 0;
}
Node *current = ptrToFirst;
Node *afterCurrent = current->getPtrToNext();
if (current->getData() == deleteData)
{
delete current;
current = afterCurrent;
if(afterCurrent != NULL)
afterCurrent = afterCurrent->getPtrToNext();
//ptrToFirst is not pointing to the new head of the list
//hence the following statement is needed.
ptrToFirst = current;
//reduce count
count--;
}
else
{
//boolean value to remember if key has been found
bool found = false;
while (afterCurrent->getPtrToNext() != nullptr)
{
if (afterCurrent->getData() == deleteData)
{
//we need current->ptrToNext to point to afterCurrent->ptrToNext
//we have to use the setPtrToNext() method
current.setPtrToNext(afterCurrent->getPtrToNext());
delete afterCurrent;
afterCurrent = current->getPtrToNext();
count--;
//update found to true;
found = true;
}
//else condition is required to move the pointer
//in case data is not yet found
else
{
current = afterCurrent;
afterCurrent = afterCurrent->getPtrToNext();
}
}
//print error message in case of key not found
if(!found)
{
cerr << "Item to be deleted is not in the list.";
}
}
}
Wherever changes or additions have been made the reason for change has been explained in the comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.