In: Computer Science
C++ Linked Lists
Don’t you love music? Everyone loves a great song playlist! Practice your
understanding of linked lists in C++ by creating 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!
Using Vector or Pointer but please write a new code don't copy the one that exist before
// C++ program to create and implement the linked list of songs
#include <iostream>
using namespace std;
// structure to represent a song in the linked list
struct Song
{
string name;
string artist;
Song *next;
};
class LinkedList
{
private:
Song *head; // pointer to the start of the linked list
public:
LinkedList();
~LinkedList();
void addSong(string name, string artist);
bool removeSong(string name);
void printList();
};
// default constructor to initialize an empty list
LinkedList::LinkedList()
{
head = nullptr;
}
// destructor to remove all nodes of the linked list
LinkedList::~LinkedList()
{
while(head != nullptr)
{
Song *temp = head;
head = head->next;
delete temp;
}
}
// function to add a song at the end of list
void LinkedList::addSong(string name, string artist)
{
Song *song = new Song;
song->name = name;
song->artist = artist;
song->next = nullptr;
if(head == nullptr)
head = song;
else
{
Song *curr = head;
while(curr->next != nullptr)
curr = curr->next;
curr->next = song;
}
}
// function to remove the song from the linked list and return of the operation was successful or not
bool LinkedList::removeSong(string name)
{
if(head != nullptr)
{
if(head->name == name)
{
Song *temp = head;
head = head->next;
delete(temp);
return true;
}else
{
Song *pre = nullptr;
Song *curr = head;
while(curr != nullptr)
{
if(curr->name == name)
{
pre->next = curr->next;
delete(curr);
return true;
}
pre = curr;
curr = curr->next;
}
return false;
}
}
return false;
}
// function to print the list
void LinkedList::printList()
{
if(head == nullptr)
{
cout<<"Empty Song List"<<endl;
}else
{
cout<<"Song List"<<endl;
Song *curr = head;
while(curr != nullptr)
{
cout<<"Name : "<<curr->name<<" Artist : "<<curr->artist<<endl;
curr = curr->next;
}
cout<<endl;
}
}
int main() {
LinkedList list;
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);
list.addSong(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(list.removeSong(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
{
list.printList();
}else if(choice != 4)
cout<<"Invalid choice"<<endl;
}while(choice != 4);
return 0;
}
//end of program
Output: