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:

Write a function that deletes a node from a singly-linked list holding

Header file:

integers. The function takes one parameter which is the integer of the

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 continue

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!