Question

In: Computer Science

Use C++ please You will be building a linked list. Make sure to keep track of...

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.

  • PlaylistNode.h - Class declaration
  • PlaylistNode.cpp - Class definition
  • main.cpp - main() function

Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps.

  • Default constructor (1 pt)
  • Parameterized constructor (1 pt)
  • Public member functions
    • InsertAfter() - Mutator (1 pt)
    • SetNext() - Mutator (1 pt)
    • 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

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:

  • Moving the head node (1 pt)
  • Moving the tail node (1 pt)
  • Moving a node to the head (1 pt)
  • Moving a node to the tail (1 pt)
  • Moving a node up the list (1 pt)
  • Moving a node down the list (1 pt)

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

Solutions

Expert Solution

// 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:


Related Solutions

You will be building a linked list. Make sure to keep track of both the head...
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 PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt) SetNext() - Mutator...
8.13 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep...
8.13 LAB: Warm up: Contacts 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. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1...
C++ Only Please 10.15 LAB: Warm up: Contacts You will be building a linked list. Make...
C++ Only Please 10.15 LAB: Warm up: Contacts 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. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext()...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
For ONE day, keep track of all of the electrical devices that you make use of...
For ONE day, keep track of all of the electrical devices that you make use of and for how long. (eg. Microwave oven for 3 minutes; hair dryer for 5 minutes; TV for 90 minutes;...) Track only those devices over which you have direct control and don’t bother about things like home heating and refrigerators that are too challenging to track. Using either a published table of common power ratings (cite your source), or information read off of the device...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Using c++ Design a system to keep track of employee data. The system should keep track...
Using c++ Design a system to keep track of employee data. The system should keep track of an employee’s name, ID number and hourly pay rate in a class called Employee. You may also store any additional data you may need, (hint: you need something extra). This data is stored in a file (user selectable) with the id number, hourly pay rate, and the employee’s full name (example): 17 5.25 Daniel Katz 18 6.75 John F. Jones Start your main...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT