Question: Please Use C PROGRAMMING and DO USE DYNAMIC MEMORY and LINKED LIST TO Solve this!!!! Objectives Dynamic memory Linked lists Description For this assignment, you
Please Use C PROGRAMMING and DO USE DYNAMIC MEMORY and LINKED LIST TO Solve this!!!!
Objectives
Dynamic memory Linked lists
Description
For this assignment, you will write a program that maintains a database of music, using a linked list.
The program has four commands: Command Purpose a title,artist Add the given song to the database. d title,artist Delete the given song from the database. l List everything in the database. q Quit the program. Sample Run
User input looks like this:
% gcc -std=c99 music.c -o music % ./music Welcome to the song database! Command: l There are no songs.
Command: a Yellow Submarine,Beatles "Yellow Submarine" by "Beatles" added.
Command: a Hey Jude,Beatles "Hey Jude" by "Beatles" added.
Command: a Mamma Mia,Abba "Mamma Mia" by "Abba" added.
Command: a Reform School Musical,Various Artists "Reform School Musical" by "Various Artists" added.
Command: l Artist Title ------ ----- Abba Mamma Mia Beatles Hey Jude Beatles Yellow Submarine Various Artists Reform School Musical
Command: d A Good Song,Eminem "A Good Song" by "Eminem" not found.
Command: d Hey Jude,Beatles "Hey Jude" by "Beatles" deleted.
Command: l Artist Title ------ ----- Abba Mamma Mia Beatles Yellow Submarine Various Artists Reform School Musical
Command: q Happy listening!
Required Stuff
You must use this structure for your linked list:
struct Song { char title[24]; char artist[24]; struct Song *next; };
You must write the following functions:
void show_all(struct Song *head) Print the entire linked list. void add_song(struct Song **ptr_to_head, char *title, char *artist) Add this song to the list. void delete_song(struct Song **ptr_to_head, char *title, char *artist) Delete this song from the list.
You may write other functions, as needed. You wont get far without writing main(). Requirements
No global variables. The songs must be stored in a dynamically-allocated linked list. show_all() takes a struct Song *, but add_song() and delete_song() take a struct Song **. Really. On input, a comma separates the title from the artist. Both the title and the artist may contain spaces. As you can see from the output, the linked list is sorted, first by artist, then by song title. Make sure to free() all the memory that you got from malloc(). This is true even if an error occurred (e.g., inserting a duplicate song). Emit an error message if the command isnt recognized, or is otherwise defective. Emit an error message if the user tries to insert the same song twice. Emit an error message if the user tries to delete something that hasnt been inserted. Emit an error message if malloc() fails. Note what the l command should emit if there are no songs. You do not have to check for a title or artist being too long.
Hints
Dont get fooled into thinking I must sort the list. You never really sort ityou just keep it sorted at all times. You can think of it as an ongoing insertion sort, kinda sorta maybe. Id read the whole command line into a string using fgets() and pick it apart using strchr(). Make sure to test the following tricky cases, because you know that we will: Inserting at the beginning of the list. Inserting at the end of the list. Deleting the first item of the list. Deleting the last item of the list.
Id do the program in this order:
Implement show_all(). Implement the parsing (picking apart) for the a command, and just use printf() to display what the title & artist are, without bothering to actually insert anything into a list. Implement add_song(), but dont insert the song in alphabetical order, at first. Just insert it at the beginning of the list. Change add_song() to insert the song in the proper place. Implement delete_song().
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
