Homework 4: Music Database
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 **head, char *title, char *artist) - Add this title to the list.
-
void delete_song(struct Song **head, char *title, char *artist) - Delete this title from the list.
You may write other functions, as needed.
You won’t get far without writing main().
Requirements
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 isn’t 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
hasn’t 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
- Don’t get fooled into thinking “I must sort the list.” You never
really sort it—you just keep it sorted at all times.
You can think of it as an ongoing insertion sort.
- I’d 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.
I’d 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.
- Implement
add_song(), but don’t 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().
Grading
This assignment is worth 10 points.
You’ll lose points if you submit a file that:
- doesn’t compile
- doesn’t work like the sample run
- isn’t named
music.c
- uses language features that we have not yet covered
- changes any of the prompts or messages
- contains poor code:
- cryptic variable names
- missing/useless comments
- extra
{ } for no reason
- and many, many, more!
How to submit your homework:
Go to the same directory as the music.c file, and do this:
~cs157/bin/checkin HW4 music.c
How to check your answer after you turn it in:
~cs157/bin/peek HW4 music.c
How to receive negative points:
Turn in someone else’s work.