Question: #include #include #include using namespace std; template class List { private: static const int CHUNK_SIZE=100; ItemType *list; int numItems; int maxItems; public: // default constructor
#include
for(int i=numItems; i>=pos; i--) list[i] = list[i-1]; // now put our item at position pos-1 list[pos-1] = item; numItems++; } return canAdd; } template
cout << "Welcome! There are " << songs.getLength() << " tracks. "; while (goAgain!='n') { trackNumber = getTrack(); try { trackName = songs.getEntry(trackNumber); } catch (invalid_argument arg) { cout << arg.what() << endl; trackName = "No Track"; } cout << "Your track name is " << trackName << endl; cout << "Go again? (y/n) "; cin >> goAgain; } cout << "Rock on! "; return 0; }
cout << "Welcome! There are " << songs.getLength() << " tracks. "; while (goAgain!='n') { trackNumber = getTrack(); try { trackName = songs.getEntry(trackNumber); }
Given in the module and familiarize yourself with it. Dont forget that list ADTs start at position 1, but the underlying array used in this class starts at position 0 like any C++ array.
Once youre comfortable with the code, you have four tasks:
void setEntry(int pos, const ItemType& item)
Replace item at position pos in the list with the new parameter item. Throw the invalid_argument exception if pos < 1 or pos > getLength().
bool remove(int pos)
Remove the item at position pos. You need to update the state of the list, that is, shift items after pos over one to get rid of the gap left by the removed item and update numItems. If the remove operation is successful, return true. If it was not successful, for example because pos is out of range, return false.
bool insert(int pos, const ItemType& item)
Also, improve the insert() function. Currently, if you try to add an item to a full list, insert() returns false and does not add the item. Improve this so that it dynamically allocates more memory to the list to make room for the new item using the new operator. You can add another CHUNK_SIZE amount of memory, or any other amount of memory you think makes sense. Don't forget to deallocate any existing memory, e.g. using delete [].
int main()
Now demonstrate that your functions work in main() by removing songs from the list and changing songs in the list. You may make modifications to the user interface to let the user do this on the fly, or just hard code a few removals/changes in main(). Make sure to demonstrate your exception handling for setEntry(). For the improved insert() function, make sure you demonstrate that the new functionality works, e.g. by changing CHUNK_SIZE to a low number like 2 and inserting more than 2 songs into the list.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
