Program 02
Based on the previous problem, you should implement this second problem in the same file a05.c.
Find the places in the code marked with the ToDo label and complete the work. You should not
change any other part of the code. This includes do not add any global variables or any other
function. For this problem, we are reusing the same struct from problem 1, but adding a pointer to
the struct to be able to create a linked list. In this problem, you must use linked list to create a user
playlist (play_list).
The struct is holding the information of a song: ID, Name, Singer, Genre, Year, and the pointer
Next (the pointer to the struct).
There should be the following functions. In the similar way we have done in previous assignments,
complete the following functions:
Add a song to the play_list - always inserted at the end of the list. Search song by name - given
the name of the song, return the struct with the data of the song.
Edit a song - given the name of the song, find it in the play_list and modify any of the fields of the
song (ID cannot be edited, and all fields should have a valid value). Return -1 if the song was not
found.
Delete a song - given the name of the song, find it, delete the song from the play_list (i.e.,
disconnect the node from the list). Return -1 if the song was not found, 1 if it was deleted.
// Problem 2
// struct to hold information about a song
struct Song
{
int id;
char name[MAX_SONG_NAME_LENGTH];
char singer[MAX_SINGER_NAME_LENGTH];
genreType genre;
int year;
struct Song *next;
};
// Function declarations
struct Song *createSong(int id, char *name, char *singer, char *genre, int year);
void printPlaylist(struct Song *playlist);
void add_song(struct Song **playlist, struct Song *newSong);
struct Song *search_song(struct Song *playlist, char *name);
int edit_song(struct Song *playlist, char *name, char *singer, char *genre, int
year);
int delete_song(struct Song *playlist, char *name);
// function to create a new song
struct Song *createSong(int id, char *name, char *singer, char *genre, int year)
{
struct Song *newSong = (struct Song *)malloc(sizeof(struct Song));
newSong->id = id;
strcpy(newSong->name, name);
strcpy(newSong->singer, singer);
if (strcmp(genre, "Pop") == 0)
newSong->genre = 1;
else if (strcmp(genre, "Rock") == 0)
newSong->genre = 2;
else if (strcmp(genre, "Reggae") == 0)
newSong->genre = 3;
else if (strcmp(genre, "Country") == 0)
newSong->genre = 4;
else if (strcmp(genre, "Blues") == 0)
newSong->genre = 5;
else if (strcmp(genre, "Balad") == 0)
newSong->genre = 6;
else
newSong->genre = 0;
newSong->year = year;
newSong->next = NULL;
return newSong;
}
// function to print the playlist
void printPlaylist(struct Song *playlist)
{
struct Song *current = playlist;
while (current != NULL)
{
printf("%dt%st%st%dt%dn", current->id, current->name, current->singer,
current->genre, current->year);
current = current->next;
}
}
// function to add a song to the end of the playlist
void add_song(struct Song .
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Program 02 Based on the previous problem you should impleme.pdf
1. Program 02
Based on the previous problem, you should implement this second problem in the same file a05.c.
Find the places in the code marked with the ToDo label and complete the work. You should not
change any other part of the code. This includes do not add any global variables or any other
function. For this problem, we are reusing the same struct from problem 1, but adding a pointer to
the struct to be able to create a linked list. In this problem, you must use linked list to create a user
playlist (play_list).
The struct is holding the information of a song: ID, Name, Singer, Genre, Year, and the pointer
Next (the pointer to the struct).
There should be the following functions. In the similar way we have done in previous assignments,
complete the following functions:
Add a song to the play_list - always inserted at the end of the list. Search song by name - given
the name of the song, return the struct with the data of the song.
Edit a song - given the name of the song, find it in the play_list and modify any of the fields of the
song (ID cannot be edited, and all fields should have a valid value). Return -1 if the song was not
found.
Delete a song - given the name of the song, find it, delete the song from the play_list (i.e.,
disconnect the node from the list). Return -1 if the song was not found, 1 if it was deleted.
// Problem 2
// struct to hold information about a song
struct Song
{
int id;
char name[MAX_SONG_NAME_LENGTH];
char singer[MAX_SINGER_NAME_LENGTH];
genreType genre;
int year;
struct Song *next;
};
// Function declarations
struct Song *createSong(int id, char *name, char *singer, char *genre, int year);
void printPlaylist(struct Song *playlist);
void add_song(struct Song **playlist, struct Song *newSong);
struct Song *search_song(struct Song *playlist, char *name);
int edit_song(struct Song *playlist, char *name, char *singer, char *genre, int
year);
int delete_song(struct Song *playlist, char *name);
// function to create a new song
struct Song *createSong(int id, char *name, char *singer, char *genre, int year)
{
struct Song *newSong = (struct Song *)malloc(sizeof(struct Song));
newSong->id = id;
2. strcpy(newSong->name, name);
strcpy(newSong->singer, singer);
if (strcmp(genre, "Pop") == 0)
newSong->genre = 1;
else if (strcmp(genre, "Rock") == 0)
newSong->genre = 2;
else if (strcmp(genre, "Reggae") == 0)
newSong->genre = 3;
else if (strcmp(genre, "Country") == 0)
newSong->genre = 4;
else if (strcmp(genre, "Blues") == 0)
newSong->genre = 5;
else if (strcmp(genre, "Balad") == 0)
newSong->genre = 6;
else
newSong->genre = 0;
newSong->year = year;
newSong->next = NULL;
return newSong;
}
// function to print the playlist
void printPlaylist(struct Song *playlist)
{
struct Song *current = playlist;
while (current != NULL)
{
printf("%dt%st%st%dt%dn", current->id, current->name, current->singer,
current->genre, current->year);
current = current->next;
}
}
// function to add a song to the end of the playlist
void add_song(struct Song **playlist, struct Song *newSong)
{
// TODO: Implement function
}
// function to search for a song by name
struct Song *search_song(struct Song *playlist, char *name)
{
// TODO: Implement function
}
// function to edit a song by name
3. int edit_song(struct Song *playlist, char *name, char *singer, char *genre, int
year)
{
// TODO: Implement function
}
// function to delete a song by name
int delete_song(struct Song *playlist, char *name)
{
// TODO: Implement function
}
void flushSngIn()
{
char c;
do
c = getchar();
while (c != 'n' && c != EOF);
}
int main()
{
// Problem 1
int choice = 0;
int songCount = 0;
char songName_input[MAX_SONG_NAME_LENGTH],
singerName_input[MAX_SINGER_NAME_LENGTH];
char genre_input[20];
unsigned int year_input, add_result = 0;
struct musicRepository music[20];
struct musicRepository *song = NULL;
initializeRepository(music, 20);
music[0].ID = numSongs + 1; numSongs++;
strcpy(music[0].songName,"Shape of You");
strcpy(music[0].singerName,"Ed Sheeran");
music[0].genre=1;
music[0].year=2017;
songCount++;
music[1].ID = numSongs + 1; numSongs++;
strcpy(music[1].songName,"Despacito");
strcpy(music[1].singerName,"Luis Fonsi");
music[1].genre=1;
music[1].year=2017;
songCount++;
music[2].ID = numSongs + 1; numSongs++;
4. strcpy(music[2].songName,"Uptown Funk");
strcpy(music[2].singerName,"Mark Ronson ft. Bruno Mars");
music[2].genre=1;
music[2].year=2014;
songCount++;
printf("Number of Songs: %d", songCount);
do
{
printf("n");
printf("1. Add a song to the repositoryn");
printf("2. Search song by namen");
printf("3. Print a songn");
printf("4. Edit a songn");
printf("5. Delete a songn");
printf("6. Print full list of songsn");
printf("7. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
flushSngIn();
switch (choice)
{
case 1:
printf("nEnter song name: ");
fgets(songName_input, sizeof(songName_input), stdin);
songName_input[strlen(songName_input) - 1] = '0';
// flushSngIn();
// printf("Song name: %sn", songName_input);
printf("Enter singer name: ");
fgets(singerName_input, sizeof(singerName_input), stdin);
singerName_input[strlen(singerName_input) - 1] = '0';
// flushSngIn();
printf("Enter genre ( Pop , Rock , Reggae , Country , Blues , Balad or
unclassified ):");
fgets(genre_input, sizeof(genre_input), stdin);
genre_input[strlen(genre_input) - 1] = '0';
// flushSngIn();
printf("Enter release year: ");
scanf("%d", &year_input);
flushSngIn();
add_result = addSong(music, songName_input, singerName_input,
genre_input, year_input, songCount);
if (add_result == 1)
5. {
printf("Song added successfullyn");
songCount++;
// printf("Number of Songs: %d", songCount);
// flushSngIn();
printRepository(music, songCount);
break;
}
case 2:
printf("Enter song name: ");
fgets(songName_input, sizeof(songName_input), stdin);
songName_input[strlen(songName_input) - 1] = '0';
flushSngIn();
song = searchSong(music, songName_input, songCount);
printf("Song found: %s, %s, %d, %d", song->songName, song->singerName,
song->genre, song->year);
flushSngIn();
break;
case 3:
if (songCount > 0)
{
int songIndex;
printf("Enter the ID of the song to print (0 to %d): ", songCount -
1);
scanf("%d", &songIndex);
printSong(music, songIndex);
flushSngIn();
break;
}
case 4:
printf("Enter song name: ");
fgets(songName_input, sizeof(songName_input), stdin);
songName_input[strlen(songName_input) - 1] = '0';
flushSngIn();
editSong(music, songCount, songName_input);
break;
case 5:
printf("Enter song name: ");
fgets(songName_input, sizeof(songName_input), stdin);
songName_input[strlen(songName_input) - 1] = '0';
flushSngIn();
deleteSongByName(music, songName_input, songCount);
6. songCount--;
printRepository(music, songCount);
break;
case 6:
printRepository(music, songCount);
break;
case 7:
printf("Exiting the programn");
break;
default:
printf("Invalid choicen");
break;
}
} while (choice != 7);
// Problem 2
// create some sample songs
struct Song *s1 = createSong(1, "Shape of You", "Ed Sheeran", "Pop", 2017);
struct Song *s2 = createSong(2, "Despacito", "Luis Fonsi", "Pop", 2017);
struct Song *s3 = createSong(3, "Uptown Funk", "Mark Ronson ft. Bruno Mars",
"Pop", 2014);
// create the playlist and add the sample songs
struct Song *playList = NULL;
add_song(&playList, s1);
add_song(&playList, s2);
add_song(&playList, s3);
// print the playlist
printf("Initial Playlist:n");
printPlaylist(playList);
// add a new song to the playlist
struct Song *s4 = createSong(4, "Sorry", "Justin Bieber", "Pop", 2015);
printf("nAdding new song to playlist:n");
add_song(&playList, s4);
printPlaylist(playList);
// delete a song from the playlist
printf("nDeleting a song from the playlist:n");
struct Song *sToDelete = search_song(playList, "Despacito");
if (sToDelete != NULL)
{
delete_song(playList, sToDelete->name);
printf("Playlist after deletion:n");
printPlaylist(playList);
}
7. else
{
printf("Song not found in playlist.n");
}
// search for a song that doesn't exist in the playlist
printf("nSearching for a song that doesn't exist in the playlist:n");
struct Song *searchResult = search_song(playList, "Non-existent Song");
if (searchResult != NULL)
{
printf("Found song:n");
printf("Song ID: %d, Song Name: %s, Singer Name: %s, Genre: %s, Year: %d",
searchResult->id, searchResult->name, searchResult->singer, searchResult->genre,
searchResult->year);
}
else
{
printf("Song not found in playlist.n");
}
// search for a song that exists in the playlist
printf("nSearching for a song that exists in the playlist:n");
searchResult = search_song(playList, "Uptown Funk");
if (searchResult != NULL)
{
// printf("Found song:n");
// printSong(searchResult);
}
else
{
printf("Song not found in playlist.n");
}
// edit a song in the playlist
printf("nEditing a song in the playlist:n");
int editResult = edit_song(playList, "Shape of You", "Ed Sheeran", "Pop",
2018);
if (editResult == -1)
{
printf("Song not found in playlist.n");
}
else
{
printf("Playlist after edit:n");
printPlaylist(playList);