The function removeAt of the class arrayListType removes an element from the lis
ID: 3855203 • Letter: T
Question
The function removeAt of the class arrayListType removes an element
from the list by shifting the elements of the list. However, if the element to be
removed is at the beginning of the list and the list is fairly large, it could take a
lot of computer time. Because the list elements are in no particular order, you
could simply remove the element by swapping the last element of the list with
the item to be removed and reducing the length of the list. Rewrite the
definition of the function removeAt using this technique.
page 204
data structrue using c++ ed. by D.S.Malik
PE1
c++
Explanation / Answer
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be removed "<< "is out of range" << endl;
else if(location < length-1)
list[location]=list[length-1];
length--;
}
Explanation:
First validate whether the entered location is within the lengt boundaries. If true, then if location entered is less than length,then swap last element with lcoation's element.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.