-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab8.c
329 lines (274 loc) · 9.34 KB
/
Lab8.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// APS105-F15 Lab 8 Lab8.c
//
// This is a program written to maintain a personal music library,
// using a linked list to hold the songs in the library.
//
// Author: <Your Name Goes Here>
// Student Number: <Your Student Number Goes Here>
//
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
// A node in the linked list
// Each string in the node is declared as a character pointer variable,
// so they need to be dynamically allocated using the malloc() function,
// and deallocated using the free() function after use.
typedef struct node {
char *songName;
char *artist;
char *genre;
struct node *link;
} Node;
// Declarations of linked list functions
//
// Declare your own linked list functions here.
//
// ADD YOUR STATEMENT(S) HERE
// Declarations of support functions
// See below the main function for descriptions of what these functions do
bool checkDuplicate(char songName[]);
void insert(char son[], char art[], char gen[]);
void printList(void);
void search(char son[]);
void delete(char son[]);
void quit(void);
void getStringFromUserInput(char s[], int arraySize);
void songNameDuplicate(char songName[]);
void songNameFound(char songName[]);
void songNameNotFound(char songName[]);
void songNameDeleted(char songName[]);
void printMusicLibraryEmpty(void);
void printMusicLibraryTitle(void);
Node *head = NULL;
const int MAX_LENGTH = 1024;
bool checkDuplicate(char songName[]) { // when duplicate return true
Node *current = head;
if (!head)
return false;
while (current != NULL) {
if (strcmp(current -> songName, songName) == 0)
return true;
current = current -> link;
}
return false;
}
void insert(char son[], char art[], char gen[]) {
Node *insert = (Node *) malloc(sizeof (Node));
insert -> songName = (char *) malloc(MAX_LENGTH * sizeof (char));
insert -> artist = (char *) malloc(MAX_LENGTH * sizeof (char));
insert -> genre = (char *) malloc(MAX_LENGTH * sizeof (char));
strcpy(insert -> songName, son);
strcpy(insert -> artist, art);
strcpy(insert -> genre, gen);
Node *current = head;
Node *prev = head;
if (!head)
head = insert; //when head is null directly change head
else if (strcmp(insert -> songName, head -> songName)<0){
head = insert;
head -> link = current; //when head is replaced
}
else {
current = current -> link;
while (current != NULL) {
if (strcmp(current->songName, insert->songName) < 0) {
prev = prev -> link;
current = current -> link; //go one step forward
} else {
prev -> link = insert;
insert -> link = current; //insert before current
return;
}
}
prev -> link = insert;
insert -> link = current;
}
}
void printList(void) {
Node *current = head;
if (!head)
printMusicLibraryEmpty(); // if nothing is in the linked list
else {
printMusicLibraryTitle();
while (current != NULL) {
printf("\n");
printf("%s\n", current -> songName);
printf("%s\n", current -> artist);
printf("%s\n", current -> genre);
current = current -> link; //print out one by one until get to null
}
}
}
void search(char son[]) {
Node *current = head;
while (current != NULL) {
if (strcmp(current -> songName, son) == 0) {
songNameFound(son);
printf("\n%s\n", son);
printf("%s\n", current -> artist);
printf("%s\n", current -> genre);
return;
} else
current = current -> link;
}
}
void delete(char son[]) {
Node *current = head;
Node *prev = head;
Node *t;
if (strcmp(head-> songName, son) == 0) {
t = head;
head = head->link;
free(t->songName);
free(t->artist);
free(t->genre);
free(t); //when delete firstNode
} else {
current = head->link;
while (current != NULL) {
if (strcmp(current->songName, son) == 0) {
prev->link = current->link;
free(current->songName);
free(current->artist);
free(current->genre);
free(current);
return;
} else {
prev = prev -> link;
current = current->link; //when delete following node
}
}
}
}
void quit(void) {
Node *current = head;
while (current != NULL) {
Node *t;
songNameDeleted(current -> songName);
t = current;
current = current -> link;
free(t -> songName);
free(t -> artist);
free(t -> genre);
free(t); //free until meet null
}
printMusicLibraryEmpty();
}
int main(void) {
// Declare the head of the linked list.
// ADD YOUR STATEMENT(S) HERE
// Announce the start of the program
printf("%s", "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 {
printf("\nCommand?: ");
getStringFromUserInput(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') {
// Insert a song into the linked list.
// Maintain the list in alphabetical order by song name.
// ADD STATEMENT(S) HERE
// USE THE FOLLOWING PRINTF STATEMENTS WHEN PROMPTING FOR DATA:
// printf("Song name: ");
// printf("Artist: ");
// printf("Genre: ");
char son[MAX_LENGTH + 1], art[MAX_LENGTH + 1], gen[MAX_LENGTH + 1];
printf("Song name: ");
getStringFromUserInput(son, MAX_LENGTH);
printf("Artist: ");
getStringFromUserInput(art, MAX_LENGTH);
printf("Genre: ");
getStringFromUserInput(gen, MAX_LENGTH);
if (checkDuplicate(son) == true)
songNameDuplicate(son);
else
insert(son, art, gen);
} else if (response == 'D') {
// Delete a song from the list.
char deleSon[MAX_LENGTH + 1];
printf("\nEnter the name of the song to be deleted: ");
getStringFromUserInput(deleSon, MAX_LENGTH);
if (checkDuplicate(deleSon) == false)
songNameNotFound(deleSon);
else {
delete(deleSon);
songNameDeleted(deleSon);
}
// ADD STATEMENT(S) HERE
} else if (response == 'S') {
// Search for a song by its name.
char searSon[MAX_LENGTH + 1];
printf("\nEnter the name of the song to search for: ");
getStringFromUserInput(searSon, MAX_LENGTH);
if (checkDuplicate(searSon) == false)
songNameNotFound(searSon);
else
search(searSon);
// ADD STATEMENT(S) HERE
} else if (response == 'P') {
// Print the music library.
printList();
// ADD STATEMENT(S) HERE
} else if (response == 'Q') {
// do nothing, we'll catch this below
quit();
} else {
// do this if no command matched ...
printf("\nInvalid command.\n");
}
} while (response != 'Q');
// Delete the entire linked list.
// ADD STATEMENT(S) HERE
// Print the linked list to confirm deletion.
// ADD STATEMENT(S) HERE
return 0;
}
// Support Function Definitions
// Prompt the user for a string safely, without buffer overflow
void getStringFromUserInput(char s[], int maxStrLength) {
int i = 0;
char c;
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");
}
// Add your functions below this line.
// ADD STATEMENT(S) HERE