Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

Students also viewed these Databases questions

Question

Is there enough time?

Answered: 1 week ago