In: Computer Science
The question is about C++
The main should test all functionalities of the playlist
implement a linked list to store songs of a playlist. A song (which is your node) is going to include the following data:
- int Song ID
- string Song Title
- string Author Title
- double Length
Don't forget to include the next pointer that will lead to the next song.
Your playlist (which is your linkedlist) is going to include the following functionalities:
- Default Constructor
- Non default constructor
- getSong(string Ttitle) that will print the information of the song object that has that specific title
- addSong() to add a new song to the end of the playlist
- removeSong() to remove a song from the beginning of the playlist
- Overload cout to display the playlist for the user
Develop a main to test your code
//C++ program
#include<iostream>
using namespace std;
typedef struct Song{
int ID;
string Title;
string Author;
double Length;
struct Song* next;
}Song;
class PlayList{
private:
Song *head;
public:
PlayList(){
head =
NULL;
}
PlayList(int id , string title ,
string author , double length){
head = new
Song;
head->ID =
id;
head->Title =
title;
head->Author
= author;
head->Length
= length;
head->next =
NULL;
}
Song* getSong(string Title){
Song*current =
head;
while(current !=
NULL){
if(current->Title == Title) return
current;
current = current->next;
}
return
NULL;
}
void addSong(int id , string title
, string author , double length){
Song* newNode =
new Song;
newNode->ID =
id;
newNode->Title = title;
newNode->Author = author;
newNode->Length = length;
newNode->next
= NULL;
if(head ==
NULL){
head = newNode;
return;
}
Song*current =
head;
while(current->next != NULL)current = current->next;
current->next
= newNode;
}
void removeSong(){
if(head == NULL)
return;
Song*current =
head;
head =
head->next;
delete
current;
}
friend ostream & operator
<< (ostream& out , const PlayList &list){
Song*current =
list.head;
while(current !=
NULL){
out<<"ID:
"<<current->ID<<endl;
out<<"Title:
"<<current->Title<<endl;
out<<"Author:
"<<current->Author<<endl;
out<<"Length:
"<<current->Length<<"\n\n";
current = current->next;
}
return
out;
}
};
int main(){
PlayList list;
list.addSong(100,"ABC","XYZ",23.90);
list.addSong(101,"JKL","XYZ",12.80);
list.addSong(102,"GHJ","XYZ",13.80);
list.addSong(103,"qwer","XYZ",34.67);
cout<<list;
Song* current = list.getSong("JKL");
if(current != NULL){
cout<<"Song found\n";
cout<<"ID:
"<<current->ID<<endl;
cout<<"Title:
"<<current->Title<<endl;
cout<<"Author:
"<<current->Author<<endl;
cout<<"Length:
"<<current->Length<<"\n\n";
}
else{
cout<<"Song not
found\n";
}
list.removeSong();
cout<<"\n\nList After Removing a song from
playList\n";
cout<<list;
return 0;
}