Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be building a linked list. there is something wrong with my insertafter function as i keep recieving this error: Tests that InsertAfter (

You will be building a linked list. there is something wrong with my insertafter function as i keep recieving this error: Tests that InsertAfter() correctly inserts "All For You"'s node after "Peg"'s node
Returned unexpected error code (-11)
Build the PlaylistNode class per the following specifications.
Default constructor (1 pt)
Parameterized constructor (1 pt)
Public member functions
InsertAfter()(1 pt)
SetNext()- Mutator (1 pt)
GetID()- Accessor
GetSongName()- Accessor
GetArtistName()- Accessor
GetSongLength()- Accessor
GetNext()- Accessor
PrintPlaylistNode()
Private data members
string uniqueID - Initialized to "none" in default constructor
string songName - Initialized to "none" in default constructor
string artistName - Initialized to "none" in default constructor
int songLength - Initialized to 0 in default constructor
PlaylistNode* nextNod#include "Playlist.h"
PlaylistNode::PlaylistNode()
{
uniqueID = "none";
songName = "none";
artistName = "none";
songLength =0;
nextNodePtr =0;
}
PlaylistNode::PlaylistNode(const string& id, const string& sname, const string& aname, int length){
uniqueID = id;
songName = sname;
artistName = aname;
songLength = length;
nextNodePtr =0;
}
void PlaylistNode::InsertAfter(PlaylistNode* ptr){
this->SetNext(ptr->GetNext());
ptr->SetNext(this);
}
void PlaylistNode::SetNext(PlaylistNode* ptr)
{
nextNodePtr = ptr;
}
Playlist::Playlist(){
head = tail = nullptr;
}
void Playlist::AddSong(const string& id, const string& sname, const string aname, int length){
PlaylistNode* temp = new PlaylistNode(id, sname, aname, length);
if (head ==0){
head = tail = temp;
} else {
temp->InsertAfter(tail);
tail = temp;
}
}
bool Playlist::RemoveSong(const string& id){
if (head == nullptr){
cout << "Playlist is empty" << endl;
return false;
}
PlaylistNode* curr = head;
PlaylistNode* prev = nullptr;
while (curr != nullptr){
if (curr->GetID()== id){
break;
}
prev = curr;
curr = curr->GetNext();
}
if (curr == nullptr){
cout <<"\""<< curr->GetSongName()<<"\" is not found" << endl;
return false;
} else {
if (prev != nullptr){
prev->SetNext(curr->GetNext());
} else {
head = curr->GetNext();
}
if (tail == curr)
tail = prev;
cout <<"\""<< curr->GetSongName()<<"\" removed." << endl;
delete curr;
return true;
}
}
bool Playlist::ChangePosition(int currPos, int newPos){
if (head == nullptr){
cout << "Playlist is empty" << endl;
return false;
}
PlaylistNode* prev = nullptr;
PlaylistNode* curr = head;
int pos;
if (head == nullptr || head == tail){
return false;
}
for (pos =1; curr != nullptr && pos < currPos; pos++){
prev = curr;
curr = curr->GetNext();
}
if (curr != nullptr){
string currentSong = curr->GetSongName();
if (prev == nullptr){
head = curr->GetNext();
} else {
prev->SetNext(curr->GetNext());
}
if (curr == tail){
tail = prev;
}
PlaylistNode* newCurr = curr;
prev = nullptr;
curr = head;
for (pos =1; curr != NULL && pos < newPos; pos++){
prev = curr;
curr = curr->GetNext();
}
if (prev == nullptr){
newCurr->SetNext(head);
head = newCurr;
} else {
newCurr->InsertAfter(prev);
}
if (curr == nullptr){
tail = newCurr;
}
cout <<"\""<< currentSong <<"\" moved to position "<< newPos << endl;
return true;
} else {
cout << "Song's current position is invalid" << endl;
return false;
}
}
void Playlist::SongsByArtist(const string& artistName) const {
if (head == nullptr){
cout << "Playlist is empty" << endl;
} else {
PlaylistNode* curr = head;
int i =1;
while (curr != nullptr){
if (curr->GetArtistName()== artistName){
cout << endl << i <<"."<< endl;
curr->PrintPlaylistNode();
}
curr = curr->GetNext();
i++;
}
}
}
int Playlist::TotalTime() const {
int total =0;
PlaylistNode* curr = head;
while (curr != nullptr){
total += curr->GetSongLength();
curr = curr->GetNext();
}
return total;
}
void Playlist::PrintList() const {
if (head == nullptr){
cout <<"
Playlist is empty" << endl;
} else {
PlaylistNode* curr = head;
int i =1;
while (curr != nullptr){
cout << endl << i++<<"."<< endl;
curr->PePtr - Initialized to 0 in default constructor

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

Create a Fishbone diagram with the problem being coal "mine safety

Answered: 1 week ago