In: Computer Science
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
Main.cpp:
/* Type your code here */
__________________
PlaylistNode.cpp
/* Type your code here */
____________________-
PlaylistNode.H
/* Type your code here */
Working code implemented in C++ and appropriate comments provided for better understanding.
Here I am attaching code for all files:
main.cpp:
#include <iostream>
using namespace std;
#include "PlaylistNode.h"
void PrintMenu(string title) {
cout << title << " PLAYLIST MENU\na - Add song\nd -
Remove song\nc - Change position of song\ns - Output songs by
specific artist\nt - Output total time of playlist (in seconds)\no
- Output full playlist\nq - Quit\n\nChoose an option:" <<
endl;
}
int main() {
string title;
string choice;
cout << "Enter playlist's title:\n" << endl;
getline(cin, title);
PrintMenu(title);
getline(cin, choice);
Playlist pl;
while(choice!="q") {
if(choice=="a") {
string id = "id";
string sName = "sName";
string aName = "aName";
int length = -1;
cout << "ADD SONG" << endl;
cout << "Enter song's unique ID:" << endl;
getline(cin, id);
cout << "Enter song's name:" << endl;
getline(cin, sName);
cout << "Enter artist's name:" << endl;
getline(cin, aName);
cout << "Enter song's length (in seconds):";
cin >> length;
cin.ignore();
PlaylistNode* tmp = new PlaylistNode(id,sName,aName,length);
if(pl.head == nullptr) {
pl.head = tmp;
pl.tail = tmp;
}
else {
PlaylistNode* current = pl.head;
bool no = true;
while(current!=nullptr && no) {
if(current->GetNext() == nullptr) {
current->InsertAfter(tmp);
pl.tail = tmp;
no = false;
}
current = current->GetNext();
}
}
cout << "\n" << endl;
}
else if(choice=="d") {
string id = "";
cout << "REMOVE SONG" << endl;
cout << "Enter song's unique ID:" << endl;
getline(cin, id);
string tmp = "tmp";
PlaylistNode* current = pl.head;
while(current!=nullptr&¤t->GetNext()!=nullptr)
{
if((current->GetNext())->GetID() == id) {
tmp = (current->GetNext())->GetSongName();
PlaylistNode* tmp2 = (current->GetNext())->GetNext();
current->SetNext( tmp2 );
}
current = current->GetNext();
}
cout << "\"" << tmp << "\"" << "
removed.\n" << endl;
}
else if(choice=="c") {
cout << "CHANGE POSITION OF SONG" << endl;
cout << "Enter song's current position:" << endl;
int cur = -1;
cin >> cur;
cout << "Enter new position for song:" << endl;
int npos = -2;
cin >> npos;
if(cur < npos) {
if(cur==1) {
PlaylistNode* temp = pl.head;
pl.head = pl.head->GetNext();
PlaylistNode* current = pl.head;
for(int i = 0; i < npos-2; i++) {
current = current->GetNext();
}
PlaylistNode* suptmp = current->GetNext();
temp->SetNext(suptmp);
current->SetNext(temp);
if(temp->GetNext() == nullptr) {
pl.tail = temp;
}
cout << "\""<<temp->GetSongName()<<"\" "
<< "moved to position " << npos << "\n" <<
endl;
}
else {
//cout << "\\\\\\\\\\\"head:
"<<pl.head->GetSongName()<<"\\\\\\\\\" " <<
endl;
PlaylistNode* current = pl.head;
for(int i = 0; i < cur-2; i++) {
current = current->GetNext();
}
PlaylistNode* temp = current->GetNext();
//cout <<
"\n\\\\\\\\\\\""<<temp->GetSongName()<<"\\\\\\\\\" "
<< endl;
PlaylistNode* suptmp = current->GetNext()->GetNext();
current->SetNext(suptmp);
current = pl.head;
for(int i = 0; i < npos-2; i++) {
current = current->GetNext();
}
PlaylistNode* xd = current->GetNext();
temp->SetNext(xd);
current->SetNext(temp);
if(temp->GetNext() == nullptr) {
pl.tail = temp;
}
cout << "\""<<temp->GetSongName()<<"\" "
<< "moved to position " << npos << "\n" <<
endl;
}
}
else if(npos<cur) {
//cout << "\\\\\\\\\\\"head:
"<<pl.head->GetSongName()<<"\\\\\\\\\" " <<
endl;
PlaylistNode* current = pl.head;
for(int i = 0; i < cur-2; i++) {
current = current->GetNext();
}
PlaylistNode* temp = current->GetNext();
//cout <<
"\n\\\\\\\\\\\""<<temp->GetSongName()<<"\\\\\\\\\" "
<< endl;
PlaylistNode* suptmp = temp->GetNext();
//cout << "\nsuptmp:
\\\\\\\\\\\""<<suptmp->GetSongName()<<"\\\\\\\\\" "
<< endl;
current->SetNext(suptmp);
if(npos == 1) {
temp->SetNext(pl.head);
pl.head = temp;
current = pl.head;
for(int i = 0; i < cur-1; i++) {
current = current->GetNext();
cout << "\ncurrent:
\\\\\\\\\\\""<<current->GetSongName()<<"\\\\\\\\\" "
<< endl;
}
current->SetNext(suptmp);
}
else {
current = pl.head;
for(int i = 0; i < npos-2; i++) {
current = current->GetNext();
}
}
PlaylistNode* xd = current->GetNext();
if(npos != 1) {
temp->SetNext(xd);
current->SetNext(temp);
}
if(temp->GetNext() == nullptr) {
pl.tail = temp;
}
cout << "\""<<temp->GetSongName()<<"\" "
<< "moved to position " << npos << "\n" <<
endl;
}
cin.ignore();
}
else if(choice=="s") {
cout << "OUTPUT SONGS BY SPECIFIC ARTIST" <<
endl;
cout << "Enter artist's name:" << endl;
string artName = "";
int counter = 0;
//cin.ignore();
getline(cin,artName);
//cout << "ARTNAME: " << artName << endl;
cout << endl;
PlaylistNode* current = pl.head;
while (current!=nullptr) {
counter++;
if(current->GetArtistName() == artName) {
cout << counter << "." << endl;
current->PrintPlaylistNode();
cout << endl;
}
current = current->GetNext();
}
}
else if(choice=="t") {
cout << "OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)" <<
endl;
int tot = 0;
PlaylistNode* current = pl.head;
while (current!=nullptr) {
tot+=current->GetSongLength();
current = current->GetNext();
}
cout << "Total time: " << tot << " seconds\n"
<< endl;
}
else if(choice=="o") {
cout << title << " - OUTPUT FULL PLAYLIST" <<
endl;
int spot = 0;
if(pl.head == nullptr)
{
cout << "Playlist is empty\n" << endl;
}
else {
PlaylistNode* current = pl.head;
while(current!=nullptr) {
spot++;
cout << spot << "." << endl;
current->PrintPlaylistNode();
cout << endl;
current = current->GetNext();
if(spot>4) {
current=nullptr;
}
}
}
}
PrintMenu(title);
getline(cin, choice);
}
return 0;
}
PlaylistNode.cpp:
#include <iostream>
#include "PlaylistNode.h"
using namespace std;
PlaylistNode::PlaylistNode() {
uniqueID = "none";
songName = "none";
artistName = "none";
songLength = 0;
nextNodePtr = 0;
}
Playlist::Playlist() {
head = nullptr;
tail = nullptr;
}
Playlist::~Playlist() {
// delete head;
// delete tail;
}
PlaylistNode::PlaylistNode(string id, string sName, string
aName, int length) {
uniqueID = id;
songName = sName;
artistName = aName;
songLength = length;
nextNodePtr = nullptr;
}
PlaylistNode::~PlaylistNode() {
// delete nextNodePtr;
}
void PlaylistNode::InsertAfter(PlaylistNode*& pl) {
this->SetNext(pl);
}
void PlaylistNode::SetNext(PlaylistNode*& pn) {
if(pn == nullptr) {
nextNodePtr = nullptr;
}
else {
nextNodePtr = pn;
}
}
string PlaylistNode::GetID() {
if(this == nullptr) {
return 0000;
}
return uniqueID;
}
string PlaylistNode::GetSongName() {
if(this == nullptr) {
return "nullSong";
}
return songName;
}
string PlaylistNode::GetArtistName() {
if(this == nullptr) {
return "nullName";
}
return artistName;
}
int PlaylistNode::GetSongLength() {
if(this == nullptr) {
return -3;
}
return songLength;
}
PlaylistNode* PlaylistNode::GetNext() {
if(this == nullptr || this->nextNodePtr == nullptr) {
return nullptr;
}
return this->nextNodePtr;
}
void PlaylistNode::PrintPlaylistNode() {
cout << "Unique ID: " << GetID() << endl;
cout << "Song Name: " << GetSongName() <<
endl;
cout << "Artist Name: " << GetArtistName() <<
endl;
cout << "Song Length (in seconds): " << GetSongLength()
<< endl;
}
PlaylistNode.h:
#ifndef PLAYLISTNODE_PLAYLISTNODE_H
#define PLAYLISTNODE_PLAYLISTNODE_H
#include <iostream>
using namespace std;
class PlaylistNode;
struct Playlist {
public:
Playlist();
~Playlist();
PlaylistNode* head;
PlaylistNode* tail;
};
class PlaylistNode {
private:
string uniqueID;
string songName;
string artistName;
int songLength;
PlaylistNode* nextNodePtr;
public:
PlaylistNode();
PlaylistNode(string id, string sName, string aName, int
length);
~PlaylistNode();
void InsertAfter(PlaylistNode*&);
void SetNext(PlaylistNode*&);
string GetID();
string GetSongName();
string GetArtistName();
int GetSongLength();
PlaylistNode* GetNext();
void PrintPlaylistNode();
};
#endif
Sample Output Screenshots: