Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

Header file:

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions