You will be building a linked list. Make sure to keep track of both the head and
ID: 3699808 • Letter: Y
Question
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
/*
* C++ Program to perform the operations on song play list using Doubly Linked List concept
*/
#include<iostream.h>
#include<cstdio.h>
#include<cstdlib.h>
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <string>
using namespace std;
class PlaylistNode {
public:
PlaylistNode(string initID, string initSongName, string initArtistName, int initSongLength, PlaylistNode *nextLoc = 0);
void SetNext(PlaylistNode *nodePtr1);
void InsertAfter(PlaylistNode *nodePtr1,*nodePtr2);
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
/*
* Main: Conatins Menu
*/
int main()
{
int choice, position;
string initID,initSongName,initArtistName;
int initSongLength,sid=0,aid=0;
PlaylistNode* nextLoc = 0;
double_llist dl;
while (1)
{
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<"Operations on Doubly linked list"<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1.Play List Node"<<endl;
cout<<"2.Set Next Song"<<endl;
cout<<"3.Insert Song After"<<endl;
cout<<"4.Get the Song ID"<<endl;
cout<<"5.Get the Song Name"<<endl;
cout<<"6.Get the Artist Name"<<endl;
cout<<"7.Check the length of the song"<<endl;
cout<<"8.Get the next songs list"<<endl;
cout<<"9.Display the list of songs:"<<endl;
cout<<"10.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the song details ID, Song Name, Init ArtistName, InitSongLength to be listed: ";
cin>>initID>initSongName,initArtistName,initSongLength;
dl.PlaylistNode(initID,initSongName,initArtistName,initSongLength,*nextLoc=0);
cout<<endl;
break;
case 2:
cout<<"Enter the song name to be set next: ";
cin>>element;
dl.SetNext(element);
cout<<endl;
break;
case 3:
cout<<"Enter the song name to be inserted next: ";
cin>>element;
cout<<"Insert Element after postion: ";
cin>>position;
dl.add_after(element, position);
cout<<endl;
break;
case 4:
d1.GetID(element);
cout<<"Enter the song name to display it`s id: ";
cin>>element;
cout<<endl;
break;
case 5:
dl.GetSongName(element);
cout<<"Enter the song name to display it`s name: ";
cin>>element;
cout<<endl;
break;
case 6:
dl.GetArtistName(element);
cout<<"Enter the song name to display it`s artist name: ";
cin>>element;
cout<<endl
break;
case 7:
d1.GetSongLength(element);
cout<<"Enter the song name to display it`s duration or length: ";
cin>>element
cout<<endl;
break;
case 8:
d1.GetNext();
cout<<endl
break;
Case 9:
d1.PrintPlaylistNode();
cout<<endl
break;
Case10:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}
/*
* Create Double Link List
*/
void double_llist::PlaylistNode(char initID,char initSongName,char initArtistName,int initSongLength,struct *nextLoc=0)
{
struct node *s, *temp;
temp = new(struct node);
temp->info = value;
temp->nextLoc = NULL;
if(start==NULL)
{
temp->initID=NULL;
initSongName=temp;
}
else
{
s=initSongName;
while (s->nextLoc!= NULL)
s=s->nextLoc;
s->nextLoc=temp;
temp->initID=s;
}
}
/*
* Song set to play in the next position
*/
void double_llist::SetNext(PlaylistNode *nodePtr1)
{
if (nodeptr1== NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp=new(struct node);
temp->prev=NULL;
temp->info=value;
temp->next=nodeptr1;
nodeptr1->prev=temp;
nodeptr1=temp;
cout<<"Element Inserted"<<endl;
}
/*
* Insertion of element at a particular position
*/
void double_llist::add_After(str element[20],int pos)
{
if (start==NULL)
{
cout<<"Create the song list."<<endl;
return;
}
struct node *tmp, *q;
int i;
q = start;
for(i=0;i<pos-1;i++)
{
q = q->next;
if (q == NULL)
{
cout<<"There are less than ";
cout<<pos<<" elements."<<endl;
return;
}
}
tmp = new(struct node);
tmp->info = value;
if (q->next == NULL)
{
q->next = tmp;
tmp->next = NULL;
tmp->prev = q;
}
else
{
tmp->next = q->next;
tmp->next->prev = tmp;
q->next = tmp;
tmp->prev = q;
}
cout<<"Song Inserted"<<endl;
}
/* Get the song ID*/
void double_llist::GetID(PlaylistNode *nodePtr1)
{
while(node!=NULL)
{
cout<<nodePtr1->data << endl;
nodePtr1=nodePtr1->next
}
nodePtr1 = last;
}
/*Display the song name*/
void double_llist:GetSongName(PlaylistNode *nodePtr1)
{
while(node!=NULL)
{
cout<<nodePtr1->data << endl;
nodePtr1=nodePtr1->next
}
nodePtr1 = last;
}
/* Display the artist name*/
void double_llist:GetArtistName(PlaylistNode *nodePtr1)
{
while(node!=NULL)
{
cout<<nodePtr1->data << endl;
nodePtr1=nodePtr1->next
}
nodePtr1 = last;
}
/*
* Get the song length
*/
void double_llist::GetSongLength(PlaylistNode *nodePtr1)
{
struct node *nodePtr1=start;
int cnt=0;
while (nodePtr1!=NULL)
{
nodePtr1=nodePtr1->next;
cnt++;
}
cout<<"length of the song is: "<<cnt<<endl;
}
/*
Get the list of next songs
*/
void double_llist::GetNext()
{
struct node *nodePtr1,*nodePtr2;
nodePtr1=start;
nodePtr2=nodePtr1->next;
nodePtr1->next=NULL;
}
/*
* Display the songs play list
*/
void double_llist::PrintPlaylistNode()
{
struct node *nodePtr1;
if (start==NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}
nodePtr1=start;
cout<<"The song list is :"<<endl;
while(nodePtr1!=NULL)
{
cout<<nodePtr1->info<<" <-> ";
nodePtr1=nodePtr1->next;
}
cout<<"NULL"<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.