-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersonalMusicLibrary.c
More file actions
260 lines (220 loc) · 7.1 KB
/
personalMusicLibrary.c
File metadata and controls
260 lines (220 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// This is a program written to maintain a personal music library,
// using a linked list to hold the songs in the library.
//
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
typedef struct node {
char * artist;
char * songName;
char * genre;
struct node * next;
} Node;
typedef struct linkedList {
Node * head;
} LinkedList;
void insertSong(LinkedList * playList, char * songName, char * artistName, char * genre);
void printPlaylist(LinkedList * playList);
void searchLinkedList(LinkedList * playList, char * songName);
void deleteSong(LinkedList * playList, char * songName);
void quit(LinkedList * playList);
void inputStringFromUser(char * prompt, char * s, int arraySize);
void songNameDuplicate(char * songName);
void songNameFound(char * songName);
void songNameNotFound(char * songName);
void songNameDeleted(char * songName);
void artistFound(char * artist);
void artistNotFound(char * artist);
void printMusicLibraryEmpty(void);
void printMusicLibraryTitle(void);
const int MAX_LENGTH = 1024;
int main(void) {
LinkedList playList;
playList.head = NULL;
printf("Personal Music Library.\n\n");
printf("%s", "Commands are I (insert), D (delete), S (search by song name),\n"
"P (print), Q (quit).\n");
char response;
char input[MAX_LENGTH + 1];
do {
inputStringFromUser("\nCommand", input, MAX_LENGTH);
// Response is the first character entered by user.
// Convert to uppercase to simplify later comparisons.
response = toupper(input[0]);
if (response == 'I') {
char songName[MAX_LENGTH + 1], artistName[MAX_LENGTH + 1], genreName[MAX_LENGTH + 1];
char * promptName = "Song name";
char * promptArtist = "Artist";
char * promptGenre = "Genre";
inputStringFromUser(promptName, songName, MAX_LENGTH);
inputStringFromUser(promptArtist, artistName, MAX_LENGTH);
inputStringFromUser(promptGenre, genreName, MAX_LENGTH);
insertSong(&playList, songName, artistName, genreName);
} else if (response == 'D') {
// Delete a song from the list.
char * prompt = "\nEnter the name of the song to be deleted";
inputStringFromUser(prompt, input, MAX_LENGTH);
deleteSong(&playList, input);
} else if (response == 'S') {
char * prompt = "\nEnter the name of the song to search for";
inputStringFromUser(prompt, input, MAX_LENGTH);
searchLinkedList(&playList, input);
} else if (response == 'P') {
// Print the music library.
printPlaylist(&playList);
} else if (response == 'Q') {
; // do nothing
quit(&playList);
} else {
// do this if no command matched ...
printf("\nInvalid command.\n");
}
} while (response != 'Q');
return 0;
}
// Prompt the user for a string safely, without buffer overflow
void inputStringFromUser(char * prompt, char * s, int maxStrLength) {
int i = 0;
char c;
printf("%s --> ", prompt);
while (i < maxStrLength && (c = getchar()) != '\n')
s[i++] = c;
s[i] = '\0';
}
// Function to call when the user is trying to insert a song name
// that is already in the personal music library.
void songNameDuplicate(char * songName) {
printf("\nA song with the name '%s' is already in the music library.\n"
"No new song entered.\n", songName);
}
// Function to call when a song name was found in the personal music library.
void songNameFound(char * songName) {
printf("\nThe song name '%s' was found in the music library.\n", songName);
}
// Function to call when a song name was not found in the personal music library.
void songNameNotFound(char * songName) {
printf("\nThe song name '%s' was not found in the music library.\n", songName);
}
// Function to call when a song name that is to be deleted
// was found in the personal music library.
void songNameDeleted(char * songName) {
printf("\nDeleting a song with name '%s' from the music library.\n", songName);
}
// Function to call when printing an empty music library.
void printMusicLibraryEmpty(void) {
printf("\nThe music library is empty.\n");
}
// Function to call to print a title when the entire music library is printed.
void printMusicLibraryTitle(void) {
printf("\nMy Personal Music Library: \n");
}
// Prints out each node of music playlist
void printPlaylist(LinkedList * playList) {
if (playList->head == NULL) {
printf("The music library is empty.\n\n");
return;
}
printf("My Personal Music Library:\n\n");
Node * currNode = playList->head;
while (currNode != NULL) {
printf("%s\n%s\n%s\n\n", currNode->songName, currNode->artist, currNode->genre);
currNode = currNode->next;
}
}
// Inserts a new song
void insertSong(LinkedList * playList, char * songName, char * artistName, char * genre) {
Node * temp = (Node *) malloc(sizeof(Node));
// Allocating space for temp variable
temp->songName = (char *)malloc((MAX_LENGTH + 1) * sizeof(char));
temp->artist = (char *)malloc((MAX_LENGTH + 1) * sizeof(char));
temp->genre = (char *)malloc((MAX_LENGTH + 1) * sizeof(char));
// Copying info
strcpy(temp->songName, songName);
strcpy(temp->artist, artistName);
strcpy(temp->genre, genre);
if (playList->head == NULL) {
temp->next = NULL;
playList->head = temp;
} else if (strcmp(playList->head->songName, songName) > 0) {
temp->next = playList->head;
playList->head = temp;
} else if (strcmp(playList->head->songName, songName) == 0) {
songNameDuplicate(songName);
} else {
Node * currNode = playList->head;
while (currNode->next != NULL && strcmp(currNode->next->songName, songName) < 0) {
currNode = currNode->next;
}
if (currNode->next == NULL) {
temp->next = NULL;
currNode->next = temp;
} else if (strcmp(currNode->next->songName, songName) == 0) {
songNameDuplicate(songName);
} else {
temp->next = currNode->next;
currNode->next = temp;
}
}
}
// Searchs for a match or no match from the playlist
void searchLinkedList(LinkedList * playList, char * songName) {
Node * currNode = playList->head;
while (currNode != NULL) {
if (strcmp(currNode->songName, songName) == 0)
songNameFound(songName);
return;
}
songNameNotFound(songName);
}
// Deletes the desired song from the playlist
void deleteSong(LinkedList * playList, char * songName) {
bool flag = false;
Node * curr = playList->head;
if (playList->head == NULL) {
printMusicLibraryEmpty();
return;
}
if (strcmp(playList->head->songName, songName) == 0) {
Node * temp = playList->head;
playList->head = playList->head->next;
free(temp->songName);
free(temp->artist);
free(temp->genre);
free(temp);
flag = true;
}
while (curr->next != NULL) {
if (strcmp(curr->next->songName, songName) == 0) {
Node * temp = curr->next;
curr->next = temp->next;
songNameDeleted(temp->songName);
free(temp->songName);
free(temp->artist);
free(temp->genre);
free(temp);
flag = true;
} else {
curr = curr->next;
}
}
if (flag == false) {
songNameNotFound(songName);
}
}
// Quits the program and frees all memory
void quit(LinkedList * playList) {
Node * ptr = playList->head;
while (ptr != NULL) {
playList->head = playList->head->next;
songNameDeleted(ptr->songName);
free(ptr->songName);
free(ptr->artist);
free(ptr->genre);
free(ptr);
ptr = playList->head;
}
printMusicLibraryEmpty();
}