Question

In: Computer Science

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

Solutions

Expert Solution

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


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 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...
(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...
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 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...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
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...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods to implement. These methods (which contain TODO comments) are found in linked_list.cpp. Part 1 Implement the copy constructor; use the LinkedBag as inspiration for your solution as the technique of creating a deep copy is essentially the same. Part 2 Implement the replace method. //linked_list.cpp #include "linked_list.h" // Header file #include <cassert> template<class Object> LinkedList<Object>::LinkedList() : headPtr( nullptr ), itemCount( 0 ) { }...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT