Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, Ive started an array-based list implementation to track songs in a playlist. Dont forget that list ADTs start at position 1, but

For this assignment, Ive started an array-based list implementation to track songs in a playlist.

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 two tasks:

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.

Here is the code:

#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 and destructor List() { numItems = 0; maxItems = CHUNK_SIZE; list = new ItemType[CHUNK_SIZE]; } ~List() { delete [] list; } // list member functions bool isEmpty() const { return numItems==0; } int getLength() const { return numItems; } bool insert(int pos, const ItemType& item); bool remove(int pos); // clear the list // clear can simply set numItems to zero. The array list may still contain // items already inserted into the list, but since numItems is zero, there // isn't any way to get at them using getEntry() or setEntry() void clear() { numItems = 0; } // return entry at postion pos // throw invalid_argument if pos<1 or pos>getLength() ItemType getEntry(int pos) const; // set entry at postion pos to item // throw invalid_argument if pos<1 or pos>getLength() void setEntry(int pos, const ItemType& item); }; template bool List::insert(int pos, const ItemType& item) { bool canAdd; canAdd = ((pos > 0) && (pos <= numItems + 1) && (numItems < maxItems)); if (canAdd) { // first, we have to move everything after our insertion point over one // position to make room for our new item. start at the back of the list. // don't forget arrays start at postion zero and our list ADT starts at // position 1. 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 ItemType List::getEntry(int pos) const { if(pos<1 || pos>getLength()) { throw invalid_argument("ERROR: getEntry() using invalid position"); } return list[pos-1]; } // TODO: add implementations for setEntry() and remove() functions // Utility function to input a track number with some input checking // to make sure it's a number int getTrack() { bool inputCheck; int trackNumber; do { inputCheck = true; cout << "Please enter the track number you'd like to view: "; if (!(cin >> trackNumber)) { cout << "Please enter numbers only. "; cin.clear(); cin.ignore(10000,' '); inputCheck = false; } } while (!inputCheck); return trackNumber; } int main() { List songs; char goAgain = 'y'; int trackNumber; string trackName; // Insert some songs into our list songs.insert(1, "One More Saturday Night"); songs.insert(1, "Friday I'm in Love"); songs.insert(3, "Sunday Morning Coming Down"); songs.insert(1, "California Love"); 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; }

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_2

Step: 3

blur-text-image_3

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

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions

Question

3. You can gain power by making others feel important.

Answered: 1 week ago