I have this programming assignment for my C++ class, and am completely lost when
ID: 3702382 • Letter: I
Question
I have this programming assignment for my C++ class, and am completely lost when it comes to single linked lists. Can someone write a code that satisfies the requirements with the use of linked list. Please leave bunch of comments so that I can further understand how the code was written. I really do appreciate the help!
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.
PlaylistNode.h - Class declaration (use file provided)
PlaylistNode.cpp - Class definition
main.cpp - main() function
Build the PlaylistNode class per the following specifications.
Default constructor
Parameterized constructor
Public member functions
InsertAfter()
SetNext() - Mutator
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* nextNodePtr - Initialized to 0 in default constructor
Note: Some functions can initially be function stubs (empty functions), to be completed in later steps.
(2) In main(), prompt the user for the title of the playlist.
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.
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
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.
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.
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:
Moving the head node
Moving the tail node
Moving a node to the head
Moving a node to the tail
Moving a node up the list
Moving a node down the list
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.
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).
Ex:
OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)
Total time: 1461 seconds
Submissions Requirements
You must use the following header file:
PlaylistNode.h
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <string>
using namespace std;
class PlaylistNode {
public:
PlaylistNode();
PlaylistNode(string initID, string initSongName, string initArtistName, int initSongLength, PlaylistNode* nextLoc = 0);
void InsertAfter(PlaylistNode* nodePtr);
void SetNext(PlaylistNode* nodePtr);
string GetID() const;
string GetSongName() const;
string GetArtistName() const;
int GetSongLength() const;
PlaylistNode* GetNext();
void PrintPlaylistNode();
private:
string uniqueID;
string songName;
string artistName;
int songLength;
PlaylistNode* nextNodePtr;
};
#endif
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
PlaylistNode.h
==============
#ifndef PlaylistNode_h
#define PlaylistNode_h
#include <iostream>
using namespace std;
class PlaylistNode
{
public:
PlaylistNode();
PlaylistNode(string id, string songname, string artist, int length);
void InsertAfter(PlaylistNode* ptr);
void SetNext(PlaylistNode* ptr);
string GetID();
string GetSongName();
string GetArtistName();
int GetSongLength();
PlaylistNode* GetNext();
void PrintPlaylistNode();
private:
string uniqueID;
string songName;
string artistName;
int songLength;
PlaylistNode* nextNodePtr;
};
#endif /* PlaylistNode_h */
PlaylistNode.cpp
==============
#include "PlaylistNode.h"
using namespace std;
PlaylistNode::PlaylistNode()
{
uniqueID = "none";
songName = "none";
artistName = "none";
songLength = 0;
nextNodePtr = 0;
}
PlaylistNode::PlaylistNode(string id, string songname, string artist, int length)
{
uniqueID = id;
songName = songname;
artistName = artist;
songLength = length;
nextNodePtr = 0;
}
void PlaylistNode::InsertAfter(PlaylistNode* ptr)
{
this->SetNext(ptr->GetNext());
ptr->SetNext(this);
}
void PlaylistNode::SetNext(PlaylistNode* ptr)
{
nextNodePtr = ptr;
}
string PlaylistNode::GetID()
{
return uniqueID;
}
string PlaylistNode::GetSongName()
{
return songName;
}
string PlaylistNode::GetArtistName()
{
return artistName;
}
int PlaylistNode::GetSongLength()
{
return songLength;
}
PlaylistNode* PlaylistNode::GetNext()
{
return nextNodePtr;
}
void PlaylistNode::PrintPlaylistNode()
{
cout << "Unique ID: " << uniqueID << endl;
cout << "Song Name: " << songName << endl;
cout << "Artist Name: " << artistName << endl;
cout << "Song Length: " << songLength << endl;
}
main.cpp
=========
#include "PlaylistNode.h"
#include <iostream>
using namespace std;
//function prototypes
void PrintMenu(string title);
void AddSong(PlaylistNode* &head, PlaylistNode* &tail, string id, string songname, string artistname, int length);
bool RemoveSong(PlaylistNode* &head, PlaylistNode* &tail, string id);
void PrintList(PlaylistNode* head);
bool ChangePosition(PlaylistNode* &head, PlaylistNode* &tail, int oldPos, int newPos);
void SongsByArtist(PlaylistNode *head, string artist);
int TotalTime(PlaylistNode *head);
int main()
{
string plTitle;
cout << "Enter playlist title: ";
getline(cin, plTitle);
PrintMenu(plTitle);
}
void PrintMenu(string title)
{
string choice = "";
string id, sname, aname;
int length, oldPos, newPos;
PlaylistNode *head = NULL, *tail = NULL;
while(choice != "q")
{
cout << title << " 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" << endl;
cout << "o - Output full playlist" << endl;
cout << "q - Quit" << endl << endl;
cout << "Choose an option: ";
cin >> choice;
cin.ignore(); //flush newline
if(choice == "a")
{
cout << "Enter song's unique ID: ";
cin >> id;
cin.ignore();//ignore newline
cout << "Enter song's name: ";
getline(cin, sname);
cout << "Enter artist's name: ";
getline(cin, aname);
cout << "Enter song's length(in seconds): ";
cin >> length;
AddSong(head, tail, id, sname, aname, length);
}
else if(choice == "d")
{
cout << "Enter song's unique ID: ";
cin >> id;
RemoveSong(head, tail, id);
}
else if(choice == "c")
{
cout << "Enter song's current position: ";
cin >> oldPos;
cout << "Enter new position for song: ";
cin >> newPos;
ChangePosition(head, tail, oldPos, newPos);
}
else if(choice == "s")
{
cout << "Enter artist's name: ";
getline(cin, aname);
SongsByArtist(head, aname);
}
else if(choice == "t")
cout << "Total time: " << TotalTime(head) << " seconds" << endl;
else if(choice == "o")
PrintList(head);
else if(choice != "q")
cout << "Invalid menu choice! Please try again." << endl;
}
}
void AddSong(PlaylistNode* &head, PlaylistNode* &tail, string id, string songname, string artistname, int length)
{
PlaylistNode* n = new PlaylistNode(id, songname, artistname, length);
if(head == 0)
head = tail = n;
else{
n->InsertAfter(tail);
tail = n;
}
}
bool RemoveSong(PlaylistNode* &head, PlaylistNode* &tail, string id)
{
PlaylistNode* curr = head;
PlaylistNode* prev = NULL;
while(curr != NULL)
{
if(curr->GetID() == id)
{
break;
}
prev = curr;
curr = curr->GetNext();
}
if(curr == NULL) //not found
return false;
else
{
if(prev != NULL)
prev->SetNext(curr->GetNext());
else
head = curr->GetNext();
if(tail == curr)
tail = prev;
cout << """ << curr->GetSongName() << "" removed"<< endl;
delete curr;
return true;
}
}
void PrintList(PlaylistNode* head)
{
int i;
if(head == NULL)
cout << "Playlist is empty." << endl;
else
{
PlaylistNode* curr = head;
i = 1;
while(curr != NULL)
{
cout << i++ << "." << endl;
curr->PrintPlaylistNode();
cout << endl;
curr = curr->GetNext();
}
}
cout << endl;
}
bool ChangePosition(PlaylistNode* &head, PlaylistNode* &tail, int oldPos, int newPos)
{
PlaylistNode* prev = NULL;
PlaylistNode* curr = head;
int pos ;
if(head == NULL || head == tail)
return false;
//first locate the node that is to be move
for(pos = 1; curr != NULL && pos < oldPos; pos++)
{
prev = curr;
curr = curr->GetNext();
}
if(curr != NULL) //found a node at given position
{
//delete it from the current list and later on insert it into the list at specified new poistion
//after removing it from the curretn list, we don;t deallocate it but simply insert it in new position
if(prev == NULL)
head = curr->GetNext();
else
{
prev->SetNext(curr->GetNext());
}
if(curr == tail) //if we removed the tail node
tail = prev;
PlaylistNode* curr1 = curr;
prev = NULL;
curr = head;
//now locate the new position
for(pos = 1; curr != NULL && pos < newPos; pos++ )
{
prev = curr;
curr = curr->GetNext();
}
//insert in located position
if(prev == NULL)
{
curr1->SetNext(head);
head = curr1;
}
else
curr1->InsertAfter(prev);
if(curr == NULL)
tail = curr1;
return true;
}
else
return false;
}
void SongsByArtist(PlaylistNode *head, string artist)
{
if(head == NULL)
cout << "Playlist is empty." << endl;
else
{
PlaylistNode* curr = head;
int i = 1;
while(curr != NULL)
{
if(curr->GetArtistName() == artist)
{
cout << i << "." << endl;
curr->PrintPlaylistNode();
cout << endl;
}
curr = curr->GetNext();
i++;
}
}
cout << endl;
}
int TotalTime(PlaylistNode *head)
{
int total = 0;
PlaylistNode* curr = head;
while(curr != NULL)
{
total += curr->GetSongLength();
curr = curr->GetNext();
}
return total;
}
output
======
Enter playlist title: JAMS
JAMS 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
o - Output full playlist
q - Quit
Choose an option: a
Enter song's unique ID: SD123
Enter song's name: Peg
Enter artist's name: Steely Dan
Enter song's length(in seconds): 237
JAMS 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
o - Output full playlist
q - Quit
Choose an option: o
1.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length: 237
JAMS 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
o - Output full playlist
q - Quit
Choose an option: a
Enter song's unique ID: JJ234
Enter song's name: All For You
Enter artist's name: Janel Jackson
Enter song's length(in seconds): 391
JAMS 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
o - Output full playlist
q - Quit
Choose an option: o
1.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length: 237
2.
Unique ID: JJ234
Song Name: All For You
Artist Name: Janel Jackson
Song Length: 391
JAMS 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
o - Output full playlist
q - Quit
Choose an option: a
Enter song's unique ID: JJ456
Enter song's name: Black Eagle
Enter artist's name: Janet Jackson
Enter song's length(in seconds): 197
JAMS 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
o - Output full playlist
q - Quit
Choose an option: o
1.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length: 237
2.
Unique ID: JJ234
Song Name: All For You
Artist Name: Janel Jackson
Song Length: 391
3.
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length: 197
JAMS 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
o - Output full playlist
q - Quit
Choose an option: s
Enter artist's name: Janet Jackson
3.
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length: 197
JAMS 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
o - Output full playlist
q - Quit
Choose an option: o
1.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length: 237
2.
Unique ID: JJ234
Song Name: All For You
Artist Name: Janel Jackson
Song Length: 391
3.
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length: 197
JAMS 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
o - Output full playlist
q - Quit
Choose an option: d
Enter song's unique ID: JJ234
"All For You" removed
JAMS 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
o - Output full playlist
q - Quit
Choose an option: t
Total time: 434 seconds
JAMS 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
o - Output full playlist
q - Quit
Choose an option: o
1.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length: 237
2.
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length: 197
JAMS 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
o - Output full playlist
q - Quit
Choose an option: q