Question

In: Computer Science

C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...

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

Solutions

Expert Solution

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


Related Solutions

C++ Linked Lists Don’t you love music? Everyone loves a great song playlist! Practice your understanding...
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!
C++ Linked Lists Don’t you love music? Everyone loves a great song playlist! Practice your understanding...
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++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
(C++) All programs will be included! This lab gives you a little practice with linked lists...
(C++) All programs will be included! This lab gives you a little practice with linked lists ·Debug insertSorted() and implement sorted() in numberlist.cpp ·Hint: insertSorted() is just missing a few parts. What is in the function can work fine once you add the right code, though you are free to rewrite it ·You need to have main.cpp, numberlist.h and numberlist.cpp in this project, and if you are using Code::Blocks, remember to set the Build Options to indicate you want to...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
Please edit the code with linked lists in C++ Please provide your BookData.txt file as well...
Please edit the code with linked lists in C++ Please provide your BookData.txt file as well BookRecord.cpp file #include "BookRecord.h" #include <stdio.h> #include <string.h> #include <iostream> #include <fstream> using namespace std; BookRecord::BookRecord() {    strcpy_s(m_sName, "");    m_lStockNum = 0;    m_iClassification = 0;    m_dCost = 0.0;    m_iCount = 0; } BookRecord::BookRecord(const char* name, long sn, int cl, double cost) {    strcpy_s(m_sName, name);    m_lStockNum = sn;    m_iClassification = cl;    m_dCost = cost;    m_iCount...
Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to...
Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to represent a tic tac toe board, write code to check if the board has a winner.
In order to practice on Linked Lists, you will implement one from scratch which will allow...
In order to practice on Linked Lists, you will implement one from scratch which will allow you to examine how they work and explore their capabilities and limitations. You will build a doubly-circular linked list. In doubly linked lists, each node in the list has a pointer to the next node and the previous node. In circular linked lists, the tail node’s next pointer refers to the head and the head node’s previous pointer refers to the tail rather than...
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;     } };...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT