-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.c
More file actions
91 lines (82 loc) · 2.63 KB
/
library.c
File metadata and controls
91 lines (82 loc) · 2.63 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
#include "library.h"
//makes 2D array of song_nodes size 27
struct song_node ** make_lib(){
struct song_node ** lib = calloc(LIB_SIZE, sizeof(struct song_node *));
return lib;
}
//adds a list to the first null entry in the library, if full just returns lib
struct song_node ** add_list(struct song_node ** lib, struct song_node * node){
lib[lib_index(node->artist)] = ordered_insert(lib[lib_index(node->artist)], node->artist, node->name);
return lib;
}
int lib_index(char *artist) {
if (!isalpha(artist[0])) return 0;
return ((int) tolower(artist[0])) - 96;
}
struct song_node * search_song(struct song_node ** lib, char * artist, char * name){
return (find_node(lib[lib_index(artist)], artist, name));
}
struct song_node * search_artist(struct song_node ** lib, char * artist){
return (find_artist(lib[lib_index(artist)], artist));
}
void print_by_letter(struct song_node ** lib, char letter){
char * letterp = &letter;
struct song_node * letter_list = lib[lib_index(letterp)];
printf("%c: ", letter);
print_list(letter_list);
}
void print_by_artist(struct song_node ** lib, char * artist){
struct song_node * letter = lib[lib_index(artist)];
printf("%s: [", artist);
while (letter) {
if (!strcmp(letter->artist, artist)) {
printf("{%s, %s} |", letter->artist, letter-> name);
}
letter = letter->next;
}
printf(" ]\n");
}
void print_lib(struct song_node ** lib){
if (lib[0]) {
printf("Other: ");
print_list(lib[0]);
}
int i = 1;
while(i < LIB_SIZE){
if (lib[i]) {
printf("%c: ", (char) (i + 96));
print_list(lib[i]);
}
i++;
}
}
void print_shuffle(struct song_node ** lib){
int i = 0;
struct song_node * node = 0;
while (i < 3) {
int index = (rand() % LIB_SIZE);
struct song_node * temp = random_node(lib[index]);
if(temp && !find_node(node,temp->artist, temp->name)){
node = insert_front(node, temp->artist, temp->name);
i++;
}
}
while (node){
struct song_node * temp = node;
print_song_node(temp);
node = node->next;
free(temp);
}
}
struct song_node ** delete_song(struct song_node ** lib, char * artist, char * name){
lib[lib_index(artist)] = remove_node(lib[lib_index(artist)], artist, name);
return lib;
}
struct song_node ** clear_lib(struct song_node ** lib){
int i = 0;
while(i < LIB_SIZE){
lib[i] = free_list(lib[i]);
i++;
}
return lib;
}