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 void UnorderedLinkedList::deleteAll(const Type& deleteItem) {

//Here

}

template

void UnorderedLinkedList::deleteSmallest()

{

{

NodeType *current = 0;

NodeType *trailCurrent = 0;

NodeType *small = 0;

NodeType *trailSmall = 0;

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: "<info<<" ";

}

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: "<info<<" ";

}

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: "<info<<" ";

}

}

}

}

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!