Write a program that simulates a playlist of songs. This will be implemented by a singly-linked list. The user will manage the playlist from a menu of options shown below. The user should then select...

1 answer below »

Write a program that simulates a playlist of songs. This will be implemented by a singly-linked list. The user will manage the playlist from a menu of options shown below.


The user should then select an option from the menu. The program should verify that a correct menu option has been selected. Once this has been done, the program should process the selection. The operation of the menu is similar to what was done for Project 01 Shopping Cart.


Significant starter code has been provided for this project. Make sure you review this before starting to code.


The following C++ files will be part of this project. Some are complete and some need modification. Please make sure you review all starter code before beginning this project. We will do an overview of these specs during our scheduled class time.


• PlaylistDriver.cpp
o File that contains the main() function
o The only function that needs to be completed is processMenuChoice. o Do not modify any other function or includes in this file.


• SongNode.h
o This file is COMPLETE – make no modifications to this file.
o This file is the declaration and definition of the SongNode class.
o This class creates and accesses SongNode objects.
o The Playlist, which is the linked list, will create the linked list of SongNode objects.


• Playlist.h
o This file is COMPLETE – make no modifications to this file. o Class declaration of the Playlist, which is the linked-list.
o Read comments included in this file to for an overview.


• Playlist.cpp
o Starter code has been provided for this.
o This is primarily the implementation of the Playlist class
o Some of the member functions have been completed – make sure you review all comments in this file in


detail to determine what you are responsible for.


Submission Requirements:




  • UML Diagram for SongNode and Playlist named P02UMLLastName




  • Sample runs of your program named P02SampleRunsLastName




  • Zipped C++project file named P02255LastName.zip




  • Put all of this into one .zip file named P02LastName.zip



Answered Same DayMar 25, 2021

Answer To: Write a program that simulates a playlist of songs. This will be implemented by a singly-linked...

Arun Shankar answered on Mar 26 2021
153 Votes
Playlist.cpp
Playlist.cpp
// Playlist.cpp
// DO NOT INCLUDE ANYTHING OTHER THAN THESE LIBRARIES/CLASSES
#include 
#include "Playlist.h"
using namespace std;
//Default and only constructor -- call private function init
Playlist::Playlist()
{
  head = nullptr;
  nodeCount = 0;
}
//Destructor -- call private function eraseList with head
Playlist::~Playlist()
{
  eraseList(head);
}
// Acce
ssor method to get the number of nodes.
int Playlist::getNodeCount()
{
  return nodeCount;
}
//Add a SongNode to the front of the list
void Playlist::insertFront(SongNode* songNode)
{
  if(head == nullptr)
    head = songNode;
  else 
  {
    songNode->setNext(head);
    head = songNode;
  }
  ++nodeCount;
}
//Remove an SongNode from the list; if the value doesn't exist in the list return false;
//   otherwise search for the SongNode, adjust pointers, and return true.
bool Playlist::remove(string id)
{
  if(!contains(id))
    return false;

  SongNode* prev = nullptr;
  SongNode* curr = head;
  if(id == head -> getUniqueID())
  {
    head = head->getNext();
    free(curr);
  }
  else
  {
    while(curr != nullptr)
    {
      if(id == curr -> getUniqueID())
      {
        prev -> setNext(curr -> getNext());
        free(curr);
        return true;
      }
      prev = curr;
      curr = curr -> getNext();
    }
  }
  return true;
}
//This function is the most complicated
// songPosition and newPosition values have been validated in main
//      before this function is called
void Playlist::changeSongPosition(int songPosition, int newPosition)
{
    // STEP 1: Find songNode that is at songPosition
  SongNode* a = head;
  SongNode* prev = nullptr;
  for(int i = 0; i < songPosition - 1; ++i)
  {
    prev = a;
    a = a -> getNext();
  }
    // STEP 2: Remove songNode at songPosition from list. Keep reference to that songNode.
  if(songPosition == 1)
    head = head -> getNext();
  else 
    prev -> setNext(a -> getNext());
    // At this point songNode is no longer in the list
    // STEP 3: Insert song at newPosition
  if(newPosition == 1)
  {
    a -> setNext(head);
    head = a;
  }
  else
  {
    SongNode* temp = head;
    for(int i = 0; i < newPosition - 2; ++i)
      temp = temp -> getNext();
    insertAfter(temp, a);
  }
}
// insertAfter ... inserts newNode after prevNode
void Playlist::insertAfter(SongNode* prevNode, SongNode* newNode)
{
  SongNode* n = prevNode->getNext();
  if(n == nullptr)
    prevNode -> setNext(newNode);
  else
  {
    newNode -> setNext(n);
    prevNode -> setNext(newNode);
  }
}
// calculate total time in seconds in playlist
int Playlist::getTotalTime()
{
  int total = 0;
  SongNode* curr = head;
  while(curr != nullptr)
  {
    total += curr -> getSongLength();
    curr = curr->getNext();
  }
  return total;
}
// Print out all nodes in the list
void Playlist::printPlaylist()
{
  printTableHeadings();
  SongNode* curr = head;
  int position = 1;
  while(curr != nullptr)
  {
    printTableRow(curr, position);
    curr = curr->getNext();
    ++position;
  }
}
// Print out all nodes in the list with the artist's name
void Playlist::printByArtist(string artist)
{
  printTableHeadings();
  SongNode* curr = head;
  int position = 1;
  while(curr != nullptr)
  {
    if(artist == curr->getArtistName())
      printTableRow(curr, position);
    curr = curr->getNext();
    ++position;
  }
}
// Search to see if list contains the uniqueID
bool Playlist::contains(const string& id)
{
  SongNode* curr = head;
  while(curr != nullptr)
  {
    if(id == curr->getUniqueID())
      return true;
    curr = curr->getNext();
  }
  return false;
}
// Clear the list -- remove all nodes and initialize the playlist again
void Playlist::clearList()
{
  eraseList(head);
  init();
}
// Set up linked list starting values
void Playlist::init()
{
  nodeCount = 0;
  head = nullptr;
}
// Delete all allocated objects
void Playlist::eraseList(SongNode* head)
{
  if(head == nullptr)
    return;
  eraseList(head->getNext());
  ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here