In: Computer Science
Use C++ please
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
(1) Create three files to submit.
Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps.
Ex. of PrintPlaylistNode output:
Unique ID: S123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237
(2) In main(), prompt the user for the title of the playlist. (1
pt)
Ex:
Enter playlist's title: JAMZ
(3) Implement the PrintMenu() function. PrintMenu() takes the
playlist title as a parameter and outputs a menu of options to
manipulate the playlist. Each option is represented by a single
character. Build and output the menu within the function.
If an invalid character is entered, continue to prompt for a
valid choice. Hint: Implement Quit before implementing other
options. Call PrintMenu() in the main() function. Continue to
execute the menu until the user enters q to Quit. (3 pts)
Ex:
JAMZ PLAYLIST MENU a - Add song d - Remove song c - Change position of song s - Output songs by specific artist t - Output total time of playlist (in seconds) o - Output full playlist q - Quit Choose an option:
(4) Implement "Output full playlist" menu option. If the list is
empty, output: Playlist is empty (3 pts)
Ex:
JAMZ - OUTPUT FULL PLAYLIST 1. Unique ID: SD123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237 2. Unique ID: JJ234 Song Name: All For You Artist Name: Janet Jackson Song Length (in seconds): 391 3. Unique ID: J345 Song Name: Canned Heat Artist Name: Jamiroquai Song Length (in seconds): 330 4. Unique ID: JJ456 Song Name: Black Eagle Artist Name: Janet Jackson Song Length (in seconds): 197 5. Unique ID: SD567 Song Name: I Got The News Artist Name: Steely Dan Song Length (in seconds): 306
(5) Implement the "Add song" menu item. New additions are added to
the end of the list. (2 pts)
Ex:
ADD SONG Enter song's unique ID: SD123 Enter song's name: Peg Enter artist's name: Steely Dan Enter song's length (in seconds): 237
(6) Implement the "Remove song" function. Prompt the user for the
unique ID of the song to be removed.(4 pts)
Ex:
REMOVE SONG Enter song's unique ID: JJ234 "All For You" removed
(7) Implement the "Change position of song" menu option. Prompt the
user for the current position of the song and the desired new
position. Valid new positions are 1 - n (the number of
nodes). If the user enters a new position that is less than 1, move
the node to the position 1 (the head). If the user enters a new
position greater than n, move the node to position
n (the tail). 6 cases will be tested:
Ex:
CHANGE POSITION OF SONG Enter song's current position: 3 Enter new position for song: 2 "Canned Heat" moved to position 2
(8) Implement the "Output songs by specific artist" menu option.
Prompt the user for the artist's name, and output the node's
information, starting with the node's current position. (2
pt)
Ex:
OUTPUT SONGS BY SPECIFIC ARTIST Enter artist's name: Janet Jackson 2. Unique ID: JJ234 Song Name: All For You Artist Name: Janet Jackson Song Length (in seconds): 391 4. Unique ID: JJ456 Song Name: Black Eagle Artist Name: Janet Jackson Song Length (in seconds): 197
(9) Implement the "Output total time of playlist" menu option.
Output the sum of the time of the playlist's songs (in seconds). (2
pts)
Ex:
OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS) Total time: 1461 seconds
// PlaylistNode.h
#ifndef PLAYLISTNODE_H
#define PLAYLISTNODE_H
#include <iostream>
#include <string>
using namespace std;
class PlaylistNode
{
private:
string uniqueID;
string songName ;
string artistName;
int songLength;
PlaylistNode* nextNodePtr;
public:
PlaylistNode();
PlaylistNode(string ID, string sname, string aname, int len);
void InsertAfter(string ID,string sname,string aname,int
len);
void SetNext(PlaylistNode *node);
string GetID();
string GetSongName();
string GetArtistName();
int GetSongLength();
PlaylistNode* GetNext();
void PrintPlaylistNode();
};
#endif
//end of PlaylistNode.h
// PlaylistNode.cpp
#include "PlaylistNode.h"
// default constructor
PlaylistNode::PlaylistNode() : uniqueID("none"), songName("none"),
artistName("none"), songLength(0), nextNodePtr(0)
{}
// Parameterized constructor
PlaylistNode::PlaylistNode(string ID, string sname, string aname,
int len)
: uniqueID(ID), songName(sname), artistName(aname),
songLength(len), nextNodePtr(0)
{}
// function to insert the given song after this node
void PlaylistNode:: InsertAfter(string ID,string sname,string
aname,int len)
{
// create a new node to store the input song details
PlaylistNode* node = new
PlaylistNode(ID,sname,aname,len);
node->SetNext(this->nextNodePtr); // update node's next to
next of this song
this->nextNodePtr = node; // update next of this song to
node
}
// updates the nextNodePtr pointer of this node to next
void PlaylistNode:: SetNext(PlaylistNode *node)
{
nextNodePtr = node;
}
// returns the songID
string PlaylistNode:: GetID()
{
return uniqueID;
}
// return the song name
string PlaylistNode:: GetSongName()
{
return songName;
}
// return the artist name
string PlaylistNode:: GetArtistName()
{
return artistName;
}
// return the song length
int PlaylistNode:: GetSongLength()
{
return songLength;
}
// return the nextNodePtr
PlaylistNode* PlaylistNode:: GetNext()
{
return nextNodePtr;
}
// function to display the song details
void PlaylistNode:: PrintPlaylistNode()
{
cout<<"Unique ID: "<<uniqueID<<endl;
cout<<"Song Name: "<<songName<<endl;
cout<<"Artist Name: "<<artistName<<endl;
cout<<"Song Length (in seconds):
"<<songLength<<endl;
}
//end of PlaylistNode.cpp
// main.cpp : C++ program to create and manipulate Playlist of songs
#include <iostream>
#include <cctype>
#include "PlaylistNode.h"
using namespace std;
// function declaration
char PrintMenu(string playlistTitle);
void AddSong(PlaylistNode*& head, PlaylistNode*&
tail);
void RemoveSong(PlaylistNode*& head, PlaylistNode*&
tail);
void MoveSong(PlaylistNode*& head, PlaylistNode*&
tail);
void SpecificArtistSong(PlaylistNode* head, PlaylistNode*
tail);
int main()
{
string playlistTitle;
PlaylistNode *head = NULL, *tail = NULL;
char choice;
// input the name of play list
cout<<"Enter playlist's title:"<<endl;
getline(cin, playlistTitle);
// loop that continues until the user exits
do
{
choice = PrintMenu(playlistTitle);
if(choice == 'a')
{
AddSong(head, tail);
}
else if(choice == 'd')
{
RemoveSong(head, tail);
}
else if(choice == 'c')
{
MoveSong(head, tail);
}
else if(choice == 's')
{
SpecificArtistSong(head, tail);
}
else if(choice == 't')
{
cout<<"OUTPUT TOTAL TIME OF PLAYLIST (IN
SECONDS)"<<endl;
int total_time = 0;
PlaylistNode *curr =head;
// loop over the list to calculate the total time of all
songs
while(curr != NULL)
{
total_time += curr->GetSongLength();
curr = curr->GetNext();
}
cout<<"Total time: "<<total_time<<"
seconds"<<endl;
}
else if(choice == 'o')
{
if(head == NULL) // empty list
cout<<"Playlist is empty"<<endl;
else
{
PlaylistNode* curr = head;
cout<<playlistTitle<<" - OUTPUT FULL
PLAYLIST"<<endl;
int pos = 1;
// loop over the list, displaying each song
while(curr != NULL)
{
cout<<pos<<". "<<endl;
curr->PrintPlaylistNode();
cout<<endl;
curr = curr->GetNext();
pos++;
}
}
}
cout<<endl;
}while(choice != 'q');
return 0;
}
// function to display the menu, input the user choice and
return it
char PrintMenu(string playlistTitle)
{
char option;
cout<<playlistTitle<<" PLAYLIST
MENU"<<endl;
cout<<"a - Add song"<<endl;
cout<<"d - Remove song"<<endl;
cout<<"c - Change position of song"<<endl;
cout<<"s - Output songs by specific
artist"<<endl;
cout<<"t - Output total time of playlist (in
seconds)"<<endl;
cout<<"o - Output full playlist"<<endl;
cout<<"q - Quit"<<endl;
cout<<endl<<"Choose an option: ";
cin>>option;
option = tolower(option);
// loop that continues until user enters a valid option
while(option != 'a' && option != 'd' && option !=
'c' && option != 's' && option != 't' &&
option != 'o' && option != 'q')
{
cout<<"Invalid Choice. Re-enter: ";
cin>>option;
option = tolower(option);
}
return option;
}
// function to add a song at the end of the list
void AddSong(PlaylistNode*& head, PlaylistNode*&
tail)
{
string uniqueID;
string songName ;
string artistName;
int songLength;
cout<<endl<<"ADD SONG"<<endl;
// input song details
cout<<"Enter song's unique ID:"<<endl;
cin>>uniqueID;
cout<<"Enter song's name:"<<endl;
cin.ignore();
getline(cin, songName);
cout<<"Enter artist's name:"<<endl;
getline(cin, artistName);
cout<<"Enter song's length (in seconds):"<<endl;
cin>>songLength;
if(head == NULL) // empty list, create a new head node and set tail
node to head
{
head = new PlaylistNode(uniqueID, songName, artistName,
songLength);;
tail = head;
}
else{
// insert the song after tail
tail->InsertAfter(uniqueID, songName, artistName,
songLength);
tail = tail->GetNext(); // update tail to the new node
created
}
}
// function to remove a song from the list
void RemoveSong(PlaylistNode*& head, PlaylistNode*&
tail)
{
string uniqueID;
cout<<endl<<"REMOVE SONG"<<endl;
// input song ID
cout<<"Enter song's unique ID:"<<endl;
cin>>uniqueID;
if(head != NULL) // non-empty list
{
PlaylistNode *curr = head;
PlaylistNode *prev = NULL;
// loop to get and remove the node with ID
while(curr != NULL)
{
if(curr->GetID() == uniqueID) // song found
{
if(prev == NULL){ // remove head node
head = head->GetNext();
if(head == NULL) // empty list after removal
tail = NULL;
}
else{
prev->SetNext(curr->GetNext());
if(prev->GetNext() == NULL) // last node removed, update tail to
prev
tail = prev;
}
cout<<"\""<<curr->GetSongName()<<"\"
removed"<<endl;
// remove current node
curr->SetNext(NULL);
delete curr;
curr = NULL;
return;
}
prev = curr;
curr = curr->GetNext();
}
}
cout<<"No Song with ID: "<<uniqueID<<" exists
in the Playlist"<<endl;
}
// function to change the position of a song
void MoveSong(PlaylistNode*& head, PlaylistNode*&
tail)
{
cout<<endl<<"CHANGE POSITION OF
SONG"<<endl;
int curr_pos, new_pos;
// input the current and new position
cout<<"Enter song's current position:"<<endl;
cin>>curr_pos;
cout<<"Enter new position for song:"<<endl;
cin>>new_pos;
int n = 0;
PlaylistNode *curr = head;
// loop to calculate the number of songs in the list
while(curr != NULL)
{
n++;
curr = curr->GetNext();
}
// validate curr_pos
if(curr_pos > 0 && curr_pos <= n){
// update new_pos to valid position
if(new_pos < 1)
new_pos = 1;
else if(new_pos > n)
new_pos = n;
if(curr_pos != new_pos) // curr_pos and new_pos are not
same
{
PlaylistNode* current;
curr = head;
PlaylistNode* prev = NULL;
int pos = 0;
// get the node to change
while(curr != NULL)
{
pos++;
if(pos == curr_pos)
{
// remove the current node from current position
current = curr;
if(prev == NULL){
head = curr->GetNext();
if(head->GetNext() == NULL)
tail = head;
}
else{
prev->SetNext(curr->GetNext());
if(prev->GetNext() == NULL)
tail = prev;
}
break;
}
prev = curr;
curr = curr->GetNext();
}
current->SetNext(NULL);
// get the node where to insert
curr = head;
prev = NULL;
pos = 1;
while((curr != NULL) && (pos != new_pos))
{
prev = curr;
curr = curr->GetNext();
pos++;
}
// insert the node in new position
current->SetNext(curr);
if(prev == NULL)
{
head = current;
}else if(prev->GetNext() == NULL)
{
tail->SetNext(current);
tail = current;
}else
prev->SetNext(current);
cout<<"\""<<current->GetSongName()<<"\"
moved to position "<<new_pos<<endl;
}
}
else
cout<<"Invalid position of current song"<<endl;
}
// function to display the songs from a given artist
void SpecificArtistSong(PlaylistNode* head, PlaylistNode*
tail)
{
string artistName;
cout<<endl<<"OUTPUT SONGS BY SPECIFIC
ARTIST"<<endl;
cin.ignore();
// input artist name
cout<<"Enter artist's name:"<<endl;
getline(cin, artistName);
PlaylistNode *curr = head;
int pos = 1;
// loop over the list, displaying the songs by the given
artist
while(curr != NULL)
{
if(curr->GetArtistName() == artistName){
cout<<pos<<"."<<endl;
curr->PrintPlaylistNode();
cout<<endl;
}
curr = curr->GetNext();
pos++;
}
}
//end of main.cpp
Output: