In: Computer Science
C++ Linked Lists
Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun!
Make sure you show your implementation of the use of vectors in this lab (You can use them too )
You MUST modularize your code ( meaning, there must be functions written for each piece of functionality that you write... at the very least: add(), delete(), print() and main() functions should be obvious.
Thanks!!
// C++ program to create and implement the linked list of songs
#include <iostream>
#include <vector>
using namespace std;
// structure to represent a song in the linked list
struct Song
{
string name;
string artist;
};
// function prototypes
void addSong(vector<Song> &songs, string name, string artist);
bool removeSong(vector<Song> &songs, string name);
void printList(vector<Song> songs);
int main() {
vector<Song> list; // linked list of songs
int choice;
string name, artist;
// loop that continues till the user exits
do
{
cout<<"1. Add a song"<<endl;
cout<<"2. Remove a song"<<endl;
cout<<"3. Print Song list"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Choice(1-4) : ";
cin>>choice;
if(choice == 1) // add a song to the linked list
{
cin.ignore(100,'\n');
// input of song name
cout<<"Enter song name : ";
getline(cin,name);
name = name.substr(0,name.length()-1);
// input of song artist
cout<<"Enter song artist : ";
getline(cin,artist);
artist = artist.substr(0,artist.length()-1);
addSong(list,name,artist);
cout<<"Song with name : "<<name<<" artist : "<<artist<<" added"<<endl;
}
else if(choice == 2) // remove the song
{
cin.ignore(100,'\n');
// input of song name
cout<<"Enter song name : ";
getline(cin,name);
name = name.substr(0,name.length()-1);
if(removeSong(list,name)) // if song was removed
{
cout<<"Song with name : "<<name<<" removed"<<endl;
}else
cout<<"Song with name : "<<name<<" doesn't exist"<<endl;
}
else if(choice == 3) // print the songs in the list
{
printList(list);
}else if(choice != 4)
cout<<"Invalid choice"<<endl;
}while(choice != 4);
return 0;
}
// function to add a new song at the end of the linked list
void addSong(vector<Song> &songs, string name, string artist)
{
Song song;
song.name = name;
song.artist = artist;
songs.push_back(song); // insert the song at the end of vector
}
// function to remove the song with name from the linked list
bool removeSong(vector<Song> &songs, string name)
{
// loop over the vector of songs
for(unsigned int i=0;i<songs.size();i++)
{
// check if name of the song at ith index is the song we want to delete
if(songs[i].name == name)
{
songs.erase(songs.begin()+i); // remove the ith song
return true; // deletion successful
}
}
return false; // delete failed
}
// function to print the list of songs
void printList(vector<Song> songs)
{
// check if list is non-empty
if(songs.size() > 0)
{
cout<<"Song List"<<endl;
// loop to print the songs
for(unsigned int i=0;i<songs.size();i++)
{
cout<<"Name : "<<songs[i].name<<" Artist : "<<songs[i].artist<<endl;
}
}else // empty list
cout<<"Empty list"<<endl;
}
//end of program
Output: