Erase all occurrences of a target in linked list
Not what you're looking for?
Extend the function eraseValue() to erase all occurrences of "target" in the linked list.
template < typename T >
void eraseAll (node * & front, const T& target);
This is the implementation of eraseValue() that must be modified.
{
node< T > *curr = front, *prev = NULL;
bool foundItem = false;
while (curr != NULL && !foundItem )
{
if (curr->nodeValue == target)
{
if (prev == NULL)
{
front = front -> next;
}
else
{
prev->next = curr-> next;
delete curr;
foundItem = true;
}
}
else
{
prev = curr;
curr = curr-> next;
}
}
Purchase this Solution
Solution Summary
The solution first mentions what needs to be modified in eraseValue function to extend it to eraseAll function, and then gives the code for eraseAll function.
Solution Preview
To erase all occurences of "target" in the linked list, you need to make following modifications to the given code.
1. Change "while (curr != NULL && !foundItem )" to "while (curr != NULL)" so that entire linked list is scanned for target value, rather than just stopping after finding first ...
Purchase this Solution
Free BrainMass Quizzes
Basic Computer Terms
We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.
Basic Networking Questions
This quiz consists of some basic networking questions.
Word 2010: Table of Contents
Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.
Inserting and deleting in a linked list
This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.
C++ Operators
This quiz tests a student's knowledge about C++ operators.