Question: In SortedListHasA.cpp, implement getPosition() so that it works correctly. It should return either the position of the given entry, if it occurs in the sorted

In SortedListHasA.cpp, implement getPosition() so that it works correctly. It should return either the position of the given entry, if it occurs in the sorted list, or the position where the entry would occur, but as a negative integer.

#include "SortedListHasA.h" // Header file #include "LinkedList.h" #include #include template SortedListHasA::SortedListHasA() { listPtr = new LinkedList(); } // end default constructor

template SortedListHasA::SortedListHasA(const SortedListHasA& sList) { // First, create our own list listPtr = new LinkedList(); // Then add items to it using public methods for(int position = 1; position <= sList.getLength(); position++) { listPtr->insert(position, sList.getEntry(position)); } // end for } // end copy constructor

template SortedListHasA::~SortedListHasA() { clear(); } // end destructor

template void SortedListHasA::insertSorted(const ItemType& newEntry) { int newPosition = fabs(getPosition(newEntry)); listPtr->insert(newPosition, newEntry); } // end insertSorted

template bool SortedListHasA::removeSorted(const ItemType& anEntry) { bool ableToRemove = false; if (!isEmpty()) { int position = getPosition(anEntry); ableToRemove = position > 0; if (ableToRemove) { ableToRemove = listPtr->remove(position); } // end if } // end if

return ableToRemove; } // end removeSorted

template int SortedListHasA::getPosition(const ItemType& anEntry) const { // this version of getPosition always returns 1. Fix it so that it implements // getPosition() correctly. It should return either the position of the given // entry, if it occurs in the sorted list, or the position where the entry // would occur, but as a negative integer. int position = 1;

return position; } // end getPosition

//===================== // List operations:

template bool SortedListHasA::remove(int position) { return listPtr->remove(position); } // end remove

template void SortedListHasA::clear() { listPtr->clear(); } // end clear

template ItemType SortedListHasA::getEntry(int position) const throw(PrecondViolatedExcep) { return listPtr->getEntry(position); } // end getEntry

template bool SortedListHasA::isEmpty() const { return listPtr->isEmpty(); } // end isEmpty

template int SortedListHasA::getLength() const { return listPtr->getLength(); } // end getLength

// End of implementation file. To get this to compile on hills, // add definitions of template types we will use in this .cpp file. // (just strings for now, add more types if desired) template class SortedListHasA; template class SortedListHasA;

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!