Question: In this program, implement getPosition() so that it works correctly. It should return either the position of the given entry, if it occurs in the
In this program, 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" #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 { 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
template class SortedListHasA; template class SortedListHasA;