Question

In: Computer Science

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 (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

Main.cpp:

/* Type your code here */

__________________

PlaylistNode.cpp

/* Type your code here */

____________________-

PlaylistNode.H

/* Type your code here */

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

Here I am attaching code for all files:

  • main.cpp
  • PlaylistNode.cpp
  • PlaylistNode.h

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&&current->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:


Related Solutions

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)...
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...
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...
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...
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()...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT