Create another list to store your Song objects using a linked list. Show that you understand how to use pointers and linked list by demonstrating the use of them with the Song objects. 8) Additional criteria for your code - Usability – your code provides users with meaningful messages/prompts for inputs and display of outputs. [3 points] - Accuracy - your code must run and terminate normally. [3 points] - Readability – your code should be well structured and easy to read. This includes: [4 points] o Indentation – Code at the same scope level should have the same amount of indentation. Each succeeding inner scope should be indented an additional four spaces or a tab. o Comments – include meaningful comments.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Song {
public:
string name;
int length;
Song *next;
Song(string n,int l) {
name = n;
length = l;
next = NULL;
}
};
class LinkedList {
public:
Song *head;
Song *tail;
LinkedList() {
head = NULL;
tail = NULL;
}
void add_node(Song *s) {
if(!head) {
head = s;
tail = s;
}
else {
tail->next = s;
tail = s;
}
cout<<(s->name)<<" added successfully"<<endl;
}
void display_all() {
Song *curr = head;
while(curr) {
cout<<"Curr song is : "<<(curr->name)<<endl;
curr = curr->next;
}
cout<<"End of list"<<endl;
}
};
int main() {
LinkedList *list = new LinkedList();
list->head = NULL;
list->tail = NULL;
Song *tmp = new Song("Hey Jude",145);
list->add_node(tmp);
tmp = new Song("The Scientist",435);
list->add_node(tmp);
tmp = new Song("Nothing else matters",342);
list->add_node(tmp);
cout<<endl;
list->display_all();
return 0;
}
Output
Hey Jude added successfully
The Scientist added successfully
Nothing else matters added successfully
Curr song is : Hey Jude
Curr song is : The Scientist
Curr song is : Nothing else matters
End of list
Screenshot
Get Answers For Free
Most questions answered within 1 hours.