Exercise 2: ( using C++) You are to code some simple, useful applications making
ID: 3756729 • Letter: E
Question
Exercise 2: ( using C++)
You are to code some simple, useful applications making appropriate use of singly- and doubly- linked lists, as described hereafter.
Refer to Lab 2, exercise 2. Repeat the exercise but now using a singly-linked list instead of an array. (Note that a doubly-linked list is not needed here as going backward is not required – since we cannot go back in time).
Create a doubly linked list for a music playlist.
Write a class Song which has title and singer as data members.
Create a doubly linked list, say MusicPlayList, of type Song.
Display a menu for the user to choose an option
Add a Song
Delete a Song
Choose a Song to Play
Forward
Backward
Exit
Exercise 2: A scout team consists of 5 children. There are 4 duties with a day off to be rotated among team members. Duties are: Fetching water, collecting firewood, cooking and clean up. You are to write a program that prompts the user for the desired number of working days and prints a schedule of daily duties for the team. Use two separate lists to store team member names and the duties The program must read the team list and task list (both comma-separated) from an input file input.txt, which may look like the example hereafter Mohd,Ali,John,Alex,Mike Water, Firewood, Cooking,Cleaning , NoDut Below is the code to read a line from a text file and separate it into tokens ifstream ifile; ifile.open("input.txt": char word[300]; ifile.getline word, 100 ); char tokenstrtok( word, "," ); while ( token != NULL ) { // Your code here token strtok( NULL, "," ); Sample Output Select Microsoft Visual Studio Debug Console Enter number of days: 6 ay 1 ay 2 ay 3 ay 4 ay 5 day 6 Water Mohd Mike Alex John Ali Mohd Firewood Ali Mohd Mike Alex John Ali Cooking John Ali Mohd Mike Alex John Cleaning Alex John Ali Mohd Mike Alex NoDuty Mike Alex John Ali Mohd MikeExplanation / Answer
#include <iostream>
#include <string.h>
using namespace std;
struct sNode //Structure for Saving Song
{
string sName;
string Singer;
struct sNode *prev;
struct sNode *next;
};
class Song //Class for Linked List
{
private:
sNode *first, *current; //First Song Name and Current Playing Song
public:
Song() //Constructor
{
first = NULL;
current = NULL;
}
bool listEmpty() //Name Suggests
{
if (first == NULL)
{
cout << "List Empty. Add Songs." << endl;
return true;
}
return false;
}
void addSong(string name, string singer) //Adding Songs
{
sNode *temp = new sNode;
temp->sName = name;
temp->Singer = singer;
temp->next = NULL;
temp->prev = NULL;
if (first == NULL) //List Empty Case
{
first = temp;
}
else //List Not Empty case
{
sNode *temp1 = first;
while (temp1->next != NULL) //Getting at the End of List
{
temp1 = temp1->next;
}
temp1->next = temp;
temp->prev = temp1;
}
}
void removeSong(int pos) //Removing by Index
{
if (listEmpty())
{
return;
}
else
{
sNode *temp = NULL;
temp = first;
int i;
for (i = 1; i < pos && temp->next != NULL; i++) //Getting at the End.
{
temp = temp->next;
}
if (i != pos) //If the position is not found
{
cout << "Song not Found" << endl;
return;
}
if (temp->next == NULL && temp->prev == NULL) //only One Element in List
{
first = NULL;
}
if (temp->next == NULL) //Last element indicated by pos
{
temp->prev->next = NULL;
}
else if (temp->prev == NULL) //First Element indicated by pos
{
first = temp->next;
first->prev = NULL;
}
else //Other element
{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
}
cout << "Song deleted " << endl;
delete temp; //Releasing the Element Memory
}
}
void removeSong(string name) //Deleting by Name .
{
if (listEmpty())
{
return;
}
sNode *temp = NULL;
temp = first;
while (temp->next != NULL && name.compare(temp->sName) != 0) //Getting the Element with Name
{
temp = temp->next;
}
if (name.compare(temp->sName) != 0) //Name not Found in the List
{
cout << "Song not Found" << endl;
return;
}
if (temp->next == NULL && temp->prev == NULL) // Following Cases are Same as Above Method
{
first = NULL;
}
if (temp->next == NULL)
{
temp->prev->next = NULL;
}
else if (temp->prev == NULL)
{
first = temp->next;
first->prev = NULL;
}
else
{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
}
cout << "Song Deleted " << endl;
delete temp;
}
void chooseSong(int pos) //Choosing by Position
{
if (listEmpty())
{
return;
}
else //List Not Empty
{
int i = 1;
sNode *temp = first;
while (temp->next != NULL && i < pos) //Getting the song node by Position
{
temp = temp->next;
i++;
}
if (i < pos) //Song Not Found
{
cout << "Song not Found " << endl;
return;
}
current = temp; //Current song is Set
songPlaying();
}
}
void chooseSong(string name) //Same as Above but with String
{
if (listEmpty())
{
return;
}
else
{
sNode *temp = first;
while (temp->next != NULL && name.compare(temp->sName) != 0)
{
temp = temp->next;
}
if (name.compare(temp->sName) != 0)
{
cout << "Song Not Found " << endl;
return;
}
current = temp;
songPlaying();
}
}
void forwardSong() //Song Forward . Changing the Current Node
{
if (listEmpty())
{
return;
}
if (current->next == NULL) //Endl of List
{
cout << "End of List." << endl;
}
else //Changing Node current
{
current = current->next;
songPlaying();
}
}
void backwardSong() //Song Backward . Changing Current Node
{
if (listEmpty())
{
return;
}
if (current->prev == NULL) //Start of List
{
cout << "This is the First Song." << endl;
}
else //Actually changing current node
{
current = current->prev;
songPlaying();
}
}
void viewList() //View List
{
if (!listEmpty())
{
sNode *temp = first;
int i = 1;
cout << "List of Songs : " << endl;
while (temp != NULL)
{
cout << i << "." << temp->sName << " by " << temp->Singer << endl;
i++;
temp = temp->next;
}
cout << endl;
}
}
void songPlaying() //Showing CUrrent Played Song
{
if (current == NULL) //No Current Song
{
cout << "No Song playing" << endl;
}
else //Song Playing
{
cout << "Song Playing : " << current->sName << " by " << current->Singer << endl;
}
}
~Song() //Destructor
{
delete first;
delete current;
}
};
//Driver Function
int main()
{
Song s1;
int ch, index;
char ch1;
string sname, singer, compNm;
do
{
cout << "1.Add a Song. 2.Delete a Song. 3.List Songs. 4.Choose a Song to Play. 5.Forward. 6.Backward. 7.Exit." << endl;
cin >> ch;
getchar();
switch (ch)
{
case 1:
cout << "Enter Song Name : ";
getline(cin, sname);
cout << " Enter Singer : ";
getline(cin, singer);
s1.addSong(sname, singer);
break;
case 2:
if (s1.listEmpty())
{
break;
}
s1.viewList();
cout << "A.Delete by song Index. B.Delete by Name " << endl;
cin >> ch1;
getchar();
switch (ch1)
{
case 'A':
case 'a':
cout << "Enter Song Index : ";
cin >> index;
s1.removeSong(index);
break;
case 'B':
case 'b':
cout << "Enter Song Name : ";
getline(cin, compNm);
s1.removeSong(compNm);
break;
}
break;
case 3:
s1.viewList();
break;
case 4:
if (s1.listEmpty())
{
break;
}
s1.viewList();
cout << "A.Choose by Index B.Choose by Name ";
cin >> ch1;
getchar();
switch (ch1)
{
case 'A':
cout << "Enter Index : ";
cin >> index;
s1.chooseSong(index);
break;
case 'B':
cout << "Enter Song Name : ";
getline(cin, compNm);
s1.chooseSong(compNm);
break;
}
break;
case 5:
s1.forwardSong();
break;
case 6:
s1.backwardSong();
break;
case 7:
exit(0);
}
} while (ch != 7);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.