Question: In C++, Alter code so that it deletes all occurances of a given element. Should be in form: template void UnorderedLinkedList ::deleteAll(const Type& deleteItem) {
In C++, Alter code so that it deletes all occurances of a given element.
Should be in form:
template
//Here
}
template
void UnorderedLinkedList
{
{
NodeType
NodeType
NodeType
NodeType
current = this->first;
if (this->first == nullptr) //list is empty.
cout << "Can not delete from an empty list." << endl;
else if (current->link == nullptr) // list has only one element, so we delete it
{
delete current;
this->first = nullptr;
this->last = nullptr;
this->count--;
cout << "deleted the single element of list ";
}
else {
trailCurrent = current;
current = current->link;
small = trailCurrent;
while (current != nullptr){
if (current->info >= small->info)
{
trailCurrent = current;
current = current->link;
}
else {
trailSmall = trailCurrent;
small = current;
trailCurrent = current;
current = current->link;
}
}
if (small == this->first) { // if smallest element is the first element
this->first = this->first->link;
delete small;
this->count--;
cout<<"Deleted first element: "<
}
else if (small == this->last) { // if smallest element is the last element
trailSmall->link = nullptr;
this->last = trailSmall;
delete small;
this->count--;
cout<<"Deleted last element: "<
}
else { // if smallest element is somewhere in middle of the list
trailSmall->link = small->link;
delete small;
this->count--;
cout<<"Deleted from middle of list: "<
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
