In: Computer Science
I need C++ code
Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required:
NOTE: Your linked list class should NOT be templated.
Example: If the input is:
3 Linda Ronstadt You're no good 2.30 Rock Elton John Rocket Man 4.41 Rock Antonin Leopold Dvorak Songs my mother taught me 2.24 Classical
where 3 is the number of songs, and each subsequent four lines is an (artist, title, length, genre) record, the output is:
Antonin Leopold Dvorak, Songs my mother taught me, 2.24, Classical Elton John, Rocket Man, 4.41, Rock Linda Ronstadt, You're no good, 2.3, Rock
Edit main to demonstrate all implemented functions!
_______________________________________________
#include <iostream>
#include <string>
#include "playlist.h"
using namespace std;
int main()
{
song tune;
string genre;
playlist mySongs;
int num = 0;
cin >> num >> ws;
for (int i=0; i<num; i++)
{
getline(cin, tune.artist);
getline(cin, tune.title);
cin >> tune.length >> ws;
getline(cin, genre);
if (genre == "Rock")
tune.genre = genre_t::ROCK;
else if (genre == "Country")
tune.genre = genre_t::COUNTRY;
else if (genre == "Pop")
tune.genre = genre_t::POP;
else if (genre == "Classical")
tune.genre = genre_t::CLASSICAL;
else
tune.genre = genre_t::POLKA;
mySongs.addSong(tune);
}
mySongs.displayList();
return 0;
}
________________________________________________
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
enum class genre_t {ROCK, COUNTRY, POP, CLASSICAL, POLKA};
struct song
{
std::string artist;
std::string title;
float length;
genre_t genre;
song* next;
};
class playlist
{
public:
// TODO: add the required member functions and operator
private:
song* head;
};
#endif
_________________________________________________________________
#include "playlist.h"
// TODO: implement the class member functions and overloaded operator
Program
//playlist.cpp
#include <iostream>
#include "playlist.h"
using namespace std;
//copy constructor
playlist::playlist(const playlist&other)
{
head = nullptr;
song *temp = other.head, *p;
while(temp!=nullptr)
{
song *newsong = new song;
newsong->artist = temp->artist;
newsong->title = temp->title;
newsong->length = temp->length;
newsong->genre = temp->genre;
if(head == nullptr)
{
head = newsong;
head->next = nullptr;
p = head;
}
else
{
p->next = newsong;
p = p->next;
}
temp = temp->next;
}
p->next = nullptr;
}
//destructor
playlist::~playlist()
{
song *temp = head;
while(temp!=nullptr)
{
temp = temp->next;
delete head;
head = temp;
}
}
//adds a single node to the front of the linked list
void playlist::addSong(song tune)
{
song *newsong = new song;
newsong->artist = tune.artist;
newsong->title = tune.title;
newsong->length = tune.length;
newsong->genre = tune.genre;
if(head == nullptr)
{
head = newsong;
head->next = nullptr;
}
else
{
newsong->next = head;
head = newsong;
}
}
//displays the linked list
void playlist::displayList()
{
song *temp = head;
while(temp!=nullptr)
{
cout<<temp->artist<<",
"<<temp->title<<",
"<<temp->length<<", ";
if(temp->genre==genre_t::ROCK)
cout<<"ROCK"<<endl;
else if(temp->genre==genre_t::COUNTRY)
cout<<"COUNTRY"<<endl;
else if(temp->genre==genre_t::POP)
cout<<"POP"<<endl;
else if(temp->genre==genre_t::CLASSICAL)
cout<<"CLASSICAL"<<endl;
else if(temp->genre==genre_t::POLKA)
cout<<"POLKA"<<endl;
temp = temp->next;
}
}
//overloaded assignment operator
playlist& playlist::operator=(const playlist&other)
{
head = nullptr;
song *temp = other.head, *p;
while(temp!=nullptr)
{
song *newsong = new song;
newsong->artist = temp->artist;
newsong->title = temp->title;
newsong->length = temp->length;
newsong->genre = temp->genre;
if(head == nullptr)
{
head = newsong;
head->next = nullptr;
p = head;
}
else
{
p->next = newsong;
p = p->next;
}
temp = temp->next;
}
p->next = nullptr;
return *this;
}
//search a song
void playlist::search(string title)
{
song *temp = head;
while(temp!=nullptr)
{
if(temp->title==title)
{
cout<<temp->artist<<",
"<<temp->title<<",
"<<temp->length<<endl;
return;
}
temp = temp->next;
}
cout<<"Not found"<<endl;
}
//delete a song
void playlist::delSong(string title)
{
song *temp = head, *p = nullptr;
while(temp!=nullptr && temp->title!=title)
{
p = temp;
temp = temp->next;
}
if(temp->title==title)
{
p->next = temp->next;
delete temp;
}
}
//insertion operator overloading
ostream& operator<<(ostream&out, const
playlist&plist)
{
song *temp = plist.head;
while(temp!=nullptr)
{
out<<temp->artist<<",
"<<temp->title<<",
"<<temp->length<<", ";
if(temp->genre==genre_t::ROCK)
cout<<"ROCK"<<endl;
else if(temp->genre==genre_t::COUNTRY)
cout<<"COUNTRY"<<endl;
else if(temp->genre==genre_t::POP)
cout<<"POP"<<endl;
else if(temp->genre==genre_t::CLASSICAL)
cout<<"CLASSICAL"<<endl;
else if(temp->genre==genre_t::POLKA)
cout<<"POLKA"<<endl;
temp = temp->next;
}
return out;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//playlist.h
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
using namespace std;
enum class genre_t {ROCK, COUNTRY, POP, CLASSICAL, POLKA};
struct song
{
std::string artist;
std::string title;
float length;
genre_t genre;
song* next;
};
class playlist
{
public:
//constructor
playlist():head(nullptr){}
//copy constructor
playlist(const playlist&other);
//destructor
~playlist();
//adds a single node to the front of the linked list
void addSong(song tune);
//displays the linked list
void displayList();
//overloaded assignment operator
playlist& operator=(const playlist&other);
//search a song
void search(string title);
//delete a song
void delSong(string title);
//insertion operator overloading
friend ostream& operator<<(ostream&, const
playlist&);
private:
song* head;
};
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//main.cpp
#include <iostream>
#include <string>
#include "playlist.h"
using namespace std;
int main()
{
song tune;
string genre;
playlist mySongs;
int num = 0;
cin >> num >> ws;
for (int i=0; i<num; i++)
{
getline(cin, tune.artist);
getline(cin, tune.title);
cin >> tune.length >> ws;
getline(cin, genre);
if (genre == "Rock")
tune.genre = genre_t::ROCK;
else if (genre == "Country")
tune.genre = genre_t::COUNTRY;
else if (genre == "Pop")
tune.genre = genre_t::POP;
else if (genre == "Classical")
tune.genre = genre_t::CLASSICAL;
else
tune.genre = genre_t::POLKA;
mySongs.addSong(tune);
}
mySongs.displayList();
cout<<"\nDemonstrate copy constructor:-
"<<endl;
playlist p2(mySongs);
p2.displayList();
cout<<"\nDemonstrate assignment and insertion operator
overloading :- "<<endl;
playlist p3(mySongs);
cout<<p3;
cout<<"\nDemonstrate searching and delete song :-
"<<endl;
p2.search("You're no good");
p2.delSong("You're no good");
cout<<p2;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Output:
3
Linda Ronstadt
You're no good
2.30
Rock
Elton John
Rocket Man
4.41
Rock
Antonin Leopold Dvorak
Songs my mother taught me
2.24
Classical
Antonin Leopold Dvorak, Songs my mother taught me, 2.24,
CLASSICAL
Elton John, Rocket Man, 4.41, ROCK
Linda Ronstadt, You're no good, 2.3, ROCK
Demonstrate copy constructor:-
Antonin Leopold Dvorak, Songs my mother taught me, 2.24,
CLASSICAL
Elton John, Rocket Man, 4.41, ROCK
Linda Ronstadt, You're no good, 2.3, ROCK
Demonstrate assignment and insertion operator overloading
:-
Antonin Leopold Dvorak, Songs my mother taught me, 2.24,
CLASSICAL
Elton John, Rocket Man, 4.41, ROCK
Linda Ronstadt, You're no good, 2.3, ROCK
Demonstrate searching and delete song :-
Linda Ronstadt, You're no good, 2.3
Antonin Leopold Dvorak, Songs my mother taught me, 2.24,
CLASSICAL
Elton John, Rocket Man, 4.41, ROCK
N.B Whether you face any problem or need any modification then share with me in the comment section, I'll happy to help you.