Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For this assignment, I ve started an array - based list implementation to track songs in a playlist. Download the code TrackSongs.cpp given in the
For this assignment, Ive started an arraybased list implementation to track songs in a playlist. Download the code TrackSongs.cpp given in the module and familiarize yourself with it Dont forget that list ADTs start at position but the underlying array used in this class starts at position like any C array.
Once youre comfortable with the code, you have four tasks:
void setEntryint pos, const ItemType& item
Replace item at position pos in the list with the new parameter item. Throw the invalidargument exception if pos or pos getLength
bool removeint 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 insertint 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 CHUNKSIZE amount of memory, or any other amount of memory you think makes sense. Don't forget to deallocate any existing memory, eg 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 removalschanges 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, eg by changing CHUNKSIZE to a low number like and inserting more than songs into the list.
Submission
Submit your updated C file with the new functions, the modified insert function, the modified main function, and sufficient sample output demonstrating all the new functionality in one plain text cpp file.
#include
#include
#include
using namespace std;
template
class List
private:
static const int CHUNKSIZE;
ItemType list;
int numItems;
int maxItems;
public:
default constructor and destructor
List numItems ; maxItems CHUNKSIZE;
list new ItemTypeCHUNKSIZE;
~List delete list;
list member functions
bool isEmpty const return numItems;
int getLength const return numItems;
bool insertint pos, const ItemType& item;
bool removeint 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 ;
return entry at postion pos
throw invalidargument if pos or posgetLength
ItemType getEntryint pos const;
set entry at postion pos to item
throw invalidargument if pos or posgetLength
void setEntryint pos, const ItemType& item;
;
template
bool List::insertint pos, const ItemType& item
bool canAdd;
canAdd pos && pos numItems && 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
forint inumItems; ipos; i
listi listi;
now put our item at position pos
listpos item;
numItems;
return canAdd;
template
ItemType List::getEntryint pos const
ifpos posgetLength
throw invalidargumentERROR: getEntry using invalid position";
return listpos;
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
;
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 "One More Saturday Night";
songs.insert "Friday Im in Love";
songs.insert "Sunday Morning Coming Down";
songs.insert "California Love";
cout "Welcome! There are songs.getLength tracks.
;
while goAgainn
trackNumber getTrack;
try
trackName songs.getEntrytrackNumbe; catch invalid argument arg cout goAgain; cout"Rock on
; return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started