this code: #include #include using namespace std; // Node struct to store data for each song in the playlist struct Node  {  string data;  Node * next; }; // Function to create a new node and return its address Node * getNewNode(string song)  {  Node * newNode = new Node();

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In this code:

#include <iostream>

#include <string>

using namespace std;
// Node struct to store data for each song in the playlist
struct Node 
{
 string data;
 Node * next;
};
// Function to create a new node and return its address
Node * getNewNode(string song) 
{
 Node * newNode = new Node();
 newNode -> data = song;
 newNode -> next = NULL;
 return newNode;
}
// Function to insert a new node at the head of the linked list
void insertAtHead(Node ** head, string song) 
{
 Node * newNode = getNewNode(song);
 if ( * head == NULL) {
  * head = newNode;
  return;
}
 newNode -> next = * head;
 * head = newNode;
}
// Function to insert a new node at the tail of the linked list
void insertAtTail(Node ** head, string song) 
{
 Node * newNode = getNewNode(song);
 if ( * head == NULL) {
  * head = newNode;
  return;
 }
 Node * temp = * head;
 while (temp -> next != NULL) 
{
  temp = temp -> next;
 }
 temp -> next = newNode;
}
// Function to remove a node from the linked list
void removeNode(Node ** head, string song)
{
 if ( * head == NULL) {
  return;
}
 Node * temp = * head;
 // If the song to be removed is at the head
 if (temp != NULL && temp -> data == song) {
  * head = temp -> next;
  free(temp);
  return;
 }
 // If the song to be removed is not at the head
 while (temp -> next != NULL) {
  if (temp -> next -> data == song) {
   break;
  }
  temp = temp -> next;
 }
 // If the song was not present in the linked list
 if (temp -> next == NULL) {
  return;
 }
 // Unlink the node from the linked list
 Node * next = temp -> next -> next;
 free(temp -> next);
 temp -> next = next;
}
// Function to print the contents of the linked list
void printList(Node * head) {
 while (head != NULL) {
  cout << head -> data << " ";
  head = head -> next;
 }
 cout << endl;
}
// Function to play the songs in the playlist in a loop
void playSongs(Node * head) {
 if (head == NULL) {
  cout << "Playlist is empty!" << endl;
  return;
 }
 cout << "Playing songs in the playlist:" << endl;
 Node * temp = head;
 while (temp != NULL) {
  cout << temp -> data << endl;
  temp = temp -> next;
 }
}
int main() {
 Node * head = NULL;
 insertAtHead( & head, "Song1");
 insertAtHead( & head, "Song2");
 insertAtTail( & head, "Song3");
 insertAtTail( & head, "Song4");
 removeNode( & head, "Song2");
 printList(head);
 playSongs(head);
 return 0;
}

how to put a traversing the playlist and allow for common operations on a playlist such as: insert, remove,  next, previous, play all songs

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education