Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;

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

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions