Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming: Search A Linked List The Content of \"songlist.txt\": Problem 2

ID: 3571886 • Letter: C

Question

C++ Programming: Search A Linked List

The Content of "songlist.txt":

Problem 2: Write a C++ program that searches the playlist (the linked list) for a certain song. There are two functions to be implemented. searchSong ByTitl e and searchSongByArtist. searchSongBy Title will search by song title and print the songs that have the title searchSongBy Artist will search by song artist and print the songs that are sung by the artist. Both functions do not need to return the song pointer and they can have a void return type.

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

#include <iostream>
#include <fstream>
#include<string>
#include<sstream>

using namespace std;

//The structure Song should contain a title, an artist, and a link for the next song.

class ListNode
{
public:
   string song;
   string artist;
   ListNode *next;

};

class SongList
{
private:
   ListNode *head; // pointer to the first node in the list
   ListNode *temp;
   int cnt; // keeps track of the number of songs in the list

public:
   SongList(); // Constructor

               // Basic Linked List Operations
   int getCount() { return cnt; }
   void insertNode(string, string);
   void displayList() const;
   void searchSongByTitle(string);
   void searchSongByArtist(string);
   void deleteSong(string);


};

//- The pointer head should point to the first song in the list.
SongList::SongList()
{
   head = NULL;
//   temp == NULL;
   cnt = 0;
}
void SongList::deleteSong(string song)
{
   if(head == NULL)
       return;

   if(head->song == song){
       head = NULL;
       return;
   }

   ListNode *temp = head;

   while(temp->next != NULL){

       if(temp->next->song == song){
           ListNode *t = temp->next;
           temp->next = temp->next->next;
           delete t;
           return;
       }

       temp = temp->next;
   }

}
void SongList::insertNode(string title, string artist)
{
   ListNode *node = new ListNode();
   //pointer
   node->song = title;
   node->artist = artist;
   node->next = NULL;
   //Each song should be inserted into the end of the linked list.
   if (head == NULL)
       head = node;
   else if (head->next == NULL)
       head->next = node;
   else
   {
       ListNode *temp = head;
       while (temp->next != NULL)
           temp = temp->next;
       temp->next = node;
   }
}
void SongList::displayList() const
{
   ListNode *temp = head;

   while (temp != NULL)
   {
       cout << temp->song << " - " << temp->artist << endl;
       temp = temp->next;
   }
}

void SongList::searchSongByTitle(string songname)
{
   ListNode *node = NULL;
   bool found;
   node = head;
   found = false;
   while (node != NULL && !found)
   {
       if (node->song == songname)
       {
           found = true;
       }
       else
       {
           node = node->next;
       }
   }
   if (found)
       cout << "This is the song you are looking for by song: " << node->song << " - " << node->artist;
   else
       cout << "Song is not Found";

}

void SongList::searchSongByArtist(string artist)
{
   ListNode *node = NULL;
   bool found;
   node = head;
   found = false;
   while (node != NULL && !found)
   {
       if (node->artist == artist)
       {
           found = true;
       }
       else
       {
           node = node->next;
       }
   }
   if (found)
       cout << "This is the song you are looking for by artist: " << node->song << " - " << node->artist;
   else
       cout << "Artist is not Found";

}

int main()
{
   string song, title, artist;
   ifstream file("songlist.txt");
   SongList songList;
   //inserts a list of songs into a music song player.
   while (getline(file, song))
   {
       // read a song per line
       istringstream iss(song);
       // iss to separate title, artist
       getline(iss, title, ' ');
       // string until , ignore
       getline(iss, artist, ' ');
       // string until end of line, ignore
       //After reading from the file, the song is inserted into the playlist (the linked list)usinga function insertSong.
       songList.insertNode(title, artist);


   }
   songList.searchSongByArtist("Wiz Khalifa feat. Charlie Puth");
   songList.searchSongByTitle("Hello");
   songList.displayList();
   getchar();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote