Exercise 2: You are to code some simple, useful applications making appropriate
ID: 3753357 • Letter: E
Question
Exercise 2:
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
Adding and deleting should update the list. Choosing a song to play will print the title and singer. Moving forward or backward will simply display the details of the previous or next songs, respectively. Exit will quit the program.
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
import java.io.*;
import java.util.*;
class DList
{
Song MusicPlayList;
static class Song
{
String title;
String singer;
Song next;
Song prev;
Song(String song_title,String singer_name)
{
title=song_title;
singer=singer_name;
}
}
// song insertion in Doubly linked list
void addSong(String song_title,String singer_name)
{
Song new_song=new Song(song_title,singer_name);
new_song.next=null;
if(MusicPlayList==null)
{
new_song.prev=null;
MusicPlayList=new_song;
return;
}
Song temp=MusicPlayList;
while(temp.next!=null)
temp=temp.next;
temp.next=new_song;
new_song.prev=temp;
System.out.println("Song has been added successfully !");
}
//print song list
void printList()
{
Song t=MusicPlayList;
if(t==null)
System.out.print("Empty");
System.out.print("Songs List : ");
int count=1;
while(t!=null)
{
System.out.print("song "+count+": Title :"+t.title+ " , Singer :"+t.singer);
if(t.next!=null)
System.out.print(" ");
t=t.next;
count++;
}
System.out.print(" ");
}
// delete song from MusicPlayList
void deleteSong(int n)
{
Song t=MusicPlayList;
int songNum=n;
if(n==1 && MusicPlayList!=null)
MusicPlayList=MusicPlayList.next;
while(n>1 && t!=null)
{
t=t.next;
n--;
}
if(t!=null)
{
t.prev=t.next;
if(t.next!=null)
t.next.prev=t.prev;
System.out.println("Song number "+songNum+" has been deleted");
}
else
{
System.out.println("Sorry ! Song "+songNum+" not found");
}
}
//Choose current song to play
void currentSong()
{
if(MusicPlayList!=null)
System.out.println(" Title :"+MusicPlayList.title+ " Singer :"+MusicPlayList.singer);
}
//Move backward in MusicPlayList
void BackwardSong()
{
if(MusicPlayList.prev!=null)
{System.out.println(" Title :"+MusicPlayList.prev.title+ " Singer :"+MusicPlayList.prev.singer);
MusicPlayList=MusicPlayList.prev;
}
}
//Move forward in MusicPlayList
void ForwardSong()
{
if(MusicPlayList.next!=null)
{System.out.println(" Title :"+MusicPlayList.next.title+ " Singer :"+MusicPlayList.next.singer);
MusicPlayList=MusicPlayList.next;
}
}
}
public class song
{
public static void main(String args[])
{
int choice,n;
String song_title,singer_name;
DList l=new DList(); // create a object of to access to linkedIntList class members
Scanner in=new Scanner(System.in);
System.out.println(" 1. Add a song 2. Delete a song 3. Choose a song to play 4. Forward 5. Backward 6.Exit Enter Choice : ");
choice=in.nextInt();
in.nextLine();
while(true)
{
switch(choice)
{
case 1 : System.out.println(" Enter title : ");
song_title=in.nextLine();
System.out.println(" Enter singer name : ");
singer_name=in.nextLine();
l.addSong(song_title,singer_name);
l.printList();
break;
case 2: System.out.println(" Enter Song number : ");
n=in.nextInt();
l.deleteSong(n);
break;
case 3: System.out.println(" Song Playing : ");
l.currentSong();
break;
case 4: System.out.println(" Next song : ");
l.ForwardSong();
break;
case 5 : System.out.println(" Previous song: ");
l.BackwardSong();
break;
case 6: return;
default : System.out.println("Invalid choice ! ");
break;
}
System.out.println(" 1. Add a song 2. Delete a song 3. Choose a song to play 4. Forward 5. Backward 6.Exit Enter Choice : ");
choice=in.nextInt();
in.nextLine();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.