-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaylist.c
709 lines (653 loc) · 19.6 KB
/
Playlist.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//Import libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <conio.h>
#include <time.h>
#define SIZE 200
// Common music structure
typedef struct music{
int id;
char tittle[SIZE];
char artist[SIZE];
char album[SIZE];
int duration;
} music;
// Tree node
typedef struct AVL_music{
music *music;
struct AVL_music *left;
struct AVL_music *right;
int h;//Height
} AVL_music;
typedef struct AVL_music *tree_AVL; //Tree - node pointer
// Circular list of musics that make up the playlist
typedef struct playlist_node {
music *music;
struct playlist_node *next;
} playlist_node;
// Simply linked list which forms the playlist list
typedef struct lplaylist_node {
int id;
char name[SIZE];
playlist_node *musics;
int amount;
struct lplaylist_node *next;
} lplaylist_node;
// Build the root of the tree
tree_AVL *build_tree_node(void){
tree_AVL *source = (tree_AVL*)malloc(sizeof(tree_AVL));
if(source != NULL){
*source = NULL;
}
return source;
}
// Head of playlist list
lplaylist_node head_playlist_list(void){
lplaylist_node *head;
head = malloc(sizeof(lplaylist_node));
head->next = NULL;
return *head;
}
// Measures the height of the node
int node_height(AVL_music *no){
if(no == NULL) return -1;
return no->h;
}
// Measures the balancing factor (difference in heights)
int balancing_factor(AVL_music *no){
return labs(node_height(no->left) - node_height(no->right));
}
// Check which number is higher
int maximum(int x, int y){
if(x > y){
return x;
}
return y;
}
// AVL tree height
int height_AVL(tree_AVL *source){
if(source == NULL) return 0;
if(*source == NULL) return 0;
int alt_left = height_AVL(&((*source)->left));
int alt_right = height_AVL(&((*source)->right));
if(alt_left > alt_right) return alt_left + 1;
else{
return alt_right + 1;
}
}
// Print playlist name and id too
void print_name_playlists(lplaylist_node *start_playlist_list) {
lplaylist_node *p;
p = malloc(sizeof(lplaylist_node));
p = start_playlist_list->next;
while(p) {
printf("\nID: %d - %s", p->id, p->name);
p = p->next;
}
}
// Search for a specific head of a playlist in the playlist list
lplaylist_node search_playlist(lplaylist_node *start_playlist_list){
int opPlay;
printf("\n\nEnter Playlist Id: ");
scanf("%d", &opPlay);
lplaylist_node *p;
p = malloc(sizeof(lplaylist_node));
p = start_playlist_list->next;
while(p && p->id != opPlay){
p = p->next;
}
return *p;
}
// Print all musics from a chosen playlist.
void print_musics_playlist(lplaylist_node *p){
playlist_node *q;
q = malloc(sizeof(playlist_node));
q = p->musics->next;
while(q != NULL){
printf("\nID - %d - %s",q->music->id ,q->music->tittle);
q = q->next;
}
}
// Print the musics with id and name
void print_musics_id_name(tree_AVL *source){
if(source == NULL){
printf("There are no musics registered");
return;
}
if(*source != NULL){
print_musics_id_name(&((*source)->left));
printf("%d - music: %s\n", (*source)->music->id, (*source)->music->tittle);
print_musics_id_name(&((*source)->right));
}
}
// Print all registered musics
void print_musics(tree_AVL *source){
if(source == NULL){
printf("There are no musics registered");
return;
}
if(*source != NULL){// Printed in order
print_musics(&((*source)->left));// Recursive call, print left elements
// Conversion to hours, minutes and seconds
int h, m, s;
h = ((*source)->music->duration) / (60*60);
m = ((*source)->music->duration - (h*60*60)) / 60;
s = ((*source)->music->duration) % 60;
printf("\n--- ID: %d ---\n", (*source)->music->id);
printf("Tittle: %s \n",(*source)->music->tittle);
printf("Artist: %s\n", (*source)->music->artist);
printf("Album: %s\n", (*source)->music->album);
printf("Duration: %02d:%02d:%02d\n", h, m, s);
printf("Balancing Factor: %d\n", balancing_factor(*source));
printf("----------------------\n");
print_musics(&((*source)->right));// And last from the right
}
}
// Do a simple right rotation
void right_rotation(tree_AVL *source){
AVL_music *auxiliary_node;
auxiliary_node = (*source)->left;
(*source)->left = auxiliary_node->right;
auxiliary_node->right = *source;
(*source)->h = maximum(node_height((*source)->left), node_height((*source)->right)) + 1;
auxiliary_node->h = maximum(node_height(auxiliary_node->left), (*source)->h) + 1;
*source = auxiliary_node;
}
// Do a simple left rotation
void left_rotation(tree_AVL *source){
AVL_music *auxiliary_node;
auxiliary_node = (*source)->right;
(*source)->right = auxiliary_node->left;
auxiliary_node->left = (*source);
(*source)->h = maximum(node_height((*source)->left), node_height((*source)->right)) + 1;
auxiliary_node->h = maximum(node_height(auxiliary_node->right), (*source)->h) + 1;
(*source) = auxiliary_node;
}
// Do a double rotation left and right
void left_right_rotation(tree_AVL *source){
left_rotation(&(*source)->left);
right_rotation(source);
}
// Do a double rotation left and right
void right_rotation_left(tree_AVL *source){
right_rotation(&(*source)->right);
left_rotation(source);
}
// Search for a music
AVL_music* search_music(tree_AVL *source, int id){
if(source == NULL){
return;
}
AVL_music *current = *source;
while(current != NULL){
if(id == current->music->id){
return current;
}
if(id > current->music->id){
current = current->right;
} else {
current = current->left;
}
}
return;
}
// Taking the chosen musics to a playlist on the keyboard selecting the musics to create a playlist
void selected_music(tree_AVL *source, lplaylist_node *start_playlist_list, int *playlist_contactor, int *playlist_id){
int musics_id[SIZE];
char entrada;
char temp[100];// It add until it finds a space or /not
int i =0;
int num = 0;
int cont = 0;
AVL_music *selected_music;
playlist_node *playlist;
playlist_node *iniPlay;
iniPlay = malloc(sizeof(playlist_node));
iniPlay->next = NULL;
iniPlay->music = NULL;
printf("\n\nSelecione as musics: ");
fflush(stdin);
do{
scanf("%c", &entrada);
temp[i] = entrada;// We changed the insert part
if (temp[i] == ' ' || temp[i] == '\n'){
temp[i] = NULL;
musics_id[num] = atoi(temp);
selected_music = malloc(sizeof(AVL_music));
selected_music = search_music(source, musics_id[num]);// Searching for music is now a function
printf("music: %s\n", selected_music->music->tittle);
playlist = malloc(sizeof(playlist_node));
playlist->next = iniPlay->next;
playlist->music = selected_music->music;
iniPlay->next = playlist;
num++;
cont ++;
i = -1;
}
i++;
} while (entrada != '\n');
criar_lista_playlist(iniPlay, playlist_contactor, start_playlist_list, playlist_id, cont);
}
// Creating the playlist list
void criar_lista_playlist(playlist_node *start_playlist, int *playlist_contactor, lplaylist_node *head, int *playlist_id, int amountmusic){
lplaylist_node *nova_lista;
(*playlist_contactor)++;
(*playlist_id) ++;
nova_lista = malloc(sizeof(lplaylist_node));
nova_lista->id = (*playlist_id);
printf("name da Playlist: ");
fflush(stdin);
gets(nova_lista->name);
nova_lista->amount= amountmusic;
nova_lista->musics = start_playlist;
nova_lista->next = head->next;
head->next = nova_lista;
}
// start of shuffle function generating a random number and a position for exchange
void function_shuffle(lplaylist_node *p){
int i;
int pos;
int num;
srand(time(NULL));
for(i = 0; i < p->amount; i++){
num = 1 + (rand() % ((p->amount)-1));
trocarmusics(num,i,p);
}
}
// We exchange the dynamic position and the index position
void trocarmusics(int randomico, int indice, lplaylist_node *head){
int i;
playlist_node *aux;
aux = malloc(sizeof(playlist_node));
playlist_node *dados;
dados = malloc(sizeof(playlist_node));
playlist_node *p;
p = malloc(sizeof(playlist_node));
playlist_node *q;
q = malloc(sizeof(playlist_node));
int cont;
cont = head->amount;
// first music playlist
dados = head->musics->next;
for(i = 0; i < cont; i ++){
if(i == randomico){
p = dados;
}
if(i == indice){
q = dados;
}
dados = dados->next;
}
aux->music = p->music;
p->music = q->music;
q->music = aux->music;
}
// Search all playlists for the desired music and delete it.
void delete_music_playlist(int id, lplaylist_node *head_playlist_list){
playlist_node *anterior;
lplaylist_node *playlist;
playlist_node *music_procurada;
playlist_node *music_procurada_salva;
music_procurada = malloc(sizeof(playlist_node));
playlist = head_playlist_list->next;
while(playlist){
music_procurada = playlist->musics->next;
anterior = playlist->musics;
while(music_procurada){
if(music_procurada->music->id == id){
(playlist->amount)--;
music_procurada_salva = music_procurada;
anterior->next = music_procurada->next;
}
anterior = music_procurada;
music_procurada = music_procurada->next;
}
playlist = playlist->next;
}
}
// Insert a music into the tree
int inserir_music(tree_AVL *source, music *nova_music){
int res;
if(*source == NULL){
AVL_music *novo;// When you only have the trunk
novo = (struct AVL_music*)malloc(sizeof(AVL_music));
if(novo == NULL){
return 0;
}
novo->music = nova_music;// we put music at the root
novo->h = 0;
novo->right = NULL;
novo->left = NULL;
*source = novo;
return 1;
}
AVL_music *current = *source;
if(nova_music->id < current->music->id){
if((res = inserir_music(&(current->left), nova_music)) == 1){
if(balancing_factor(current) >= 2){
if(nova_music->id < (*source)->left->music->id){
right_rotation(source);
}else{
left_right_rotation(source);
}
}
}
}else{
if(nova_music->id > current->music->id){
if((res = inserir_music(&(current->right),nova_music)) == 1){
if(balancing_factor(current) >= 2){
if((*source)->right->music->id < nova_music->id){
left_rotation(source);
}else{
right_rotation_left(source);
}
}
}
}else{// If the value is the same, there will be no insertion
printf("Valor duplicado");
return 0;
}
}
current->h = maximum(node_height(current->left),node_height(current->right)) + 1;
return res;
}
// Look for the smallest in the
AVL_music *procura_menor(AVL_music *current){
AVL_music *no1 = current;
AVL_music *no2 = current->left;
while(no2 != NULL){
no1 = no2;
no2 = no2->left;
}
return no1;
}
// Delete a music
int delete_music( tree_AVL *source, int id, int *music_contactor){
if((*source) == NULL){
printf("music nAo cadastrada\n");
return 0;
}
int res;
if(id < (*source)->music->id){
if((res = delete_music(&(*source)->left, id, music_contactor)) == 1){
if(balancing_factor(*source) >= 2){
if(node_height((*source)->right->left) <= node_height((*source)->right->right)){
left_rotation(source);
}else{
left_right_rotation(source);
}
}
}
}
if((*source)->music->id < id){
if((res = delete_music(&(*source)->right, id, music_contactor)) == 1){
if(balancing_factor(*source) >= 2){
if(node_height((*source)->left->right) <= node_height((*source)->left->left)){
right_rotation(source);
}else{
right_rotation_left(source);
}
}
}
}
if((*source)->music->id == id){
if(((*source)->left == NULL || (*source)->right == NULL)){
AVL_music *antigo = (*source);
if((*source)->left != NULL){
*source = (*source)->left;
}else{
*source = (*source)->right;
}
free(antigo);
}else{
AVL_music *temp = procura_menor((*source)->right);
(*source)->music = temp->music;
res = delete_music(&(*source)->right,(*source)->music->id, music_contactor);
if(balancing_factor(*source) >= 2){
if(node_height((*source)->left->right) <= node_height((*source)->left->left)){
right_rotation(source);
}else{
right_rotation_left(source);
}
}
}
return 1;
}
(*source)->h = maximum(node_height((*source)->left),node_height((*source)->right)) + 1;// In any situation it updates the height
if(res == 1){
(*music_contactor)--; // If it is eliminated we reduce the amount of musics
}
return res;
}
// Registering a music
void register_music(int *music_contactor, tree_AVL *source, int *music_id){
int res;
music *nova;
nova = malloc(sizeof(music));
*music_contactor += 1;
*music_id += 1;
nova->id = *music_id;
printf("\ntittle: ");
fflush(stdin);
gets(nova->tittle);
printf("artist: ");
fflush(stdin);
gets(nova->artist);
printf("Album: ");
fflush(stdin);
gets(nova->album);
printf("duration em Segundos: ");
scanf("%d", &nova->duration);
res = inserir_music(source, nova);// inserting music
}
// create music for testing
void create_test_musics(int *music_contactor, tree_AVL *source, int *music_id) {
music *nova;
nova = malloc(sizeof(music));
(*music_contactor)++;
(*music_id)++;
nova->id = *music_id;
strcpy(nova->tittle, "Girassol");
strcpy(nova->artist, "Cidade Negra");
strcpy(nova->album, "Acustico Cidade Negra");
nova->duration = 111;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_id)++;
(*music_contactor)++;
nova->id = *music_id;
strcpy(nova->tittle, "Rindo a Toa");
strcpy(nova->artist, "Falamanca");
strcpy(nova->album, "Falamanca Essencial");
nova->duration = 122;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_id)++;
(*music_contactor)++;
nova->id = *music_id;
strcpy(nova->tittle, "Enquanto Houver Sol");
strcpy(nova->artist, "Titas");
strcpy(nova->album, "Como Estao Voces?");
nova->duration = 133;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_id)++;
(*music_contactor)++;
nova->id = *music_id;
strcpy(nova->tittle, "Reconvexo");
strcpy(nova->artist, "Caetano Veloso");
strcpy(nova->album, "Ofertario");
nova->duration = 144;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_id)++;
(*music_contactor)++;
nova->id = *music_id;
strcpy(nova->tittle, "A Paz");
strcpy(nova->artist, "Gilberto Gil");
strcpy(nova->album, "Unplugged");
nova->duration = 155;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_contactor)++;
(*music_id)++;
nova->id = *music_id;
strcpy(nova->tittle, "Um Sonho");
strcpy(nova->artist, "Nacao Zumbi");
strcpy(nova->album, "Nacao Zumbi");
nova->duration = 111;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_id)++;
(*music_contactor)++;
nova->id = *music_id;
strcpy(nova->tittle, "Gente Aberta");
strcpy(nova->artist, "Erasmo Carlos");
strcpy(nova->album, "Carlos, Erasmo");
nova->duration = 122;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_id)++;
(*music_contactor)++;
nova->id = *music_id;
strcpy(nova->tittle, "Estado de Poesia");
strcpy(nova->artist, "Chico Cesar");
strcpy(nova->album, "Estado de Poesia");
nova->duration = 133;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_id)++;
(*music_contactor)++;
nova->id = *music_id;
strcpy(nova->tittle, "Sorte");
strcpy(nova->artist, "Ney Matogrosso");
strcpy(nova->album, "Sorte");
nova->duration = 144;
inserir_music(source, nova);
nova = malloc(sizeof(music));
(*music_id)++;
(*music_contactor)++;
nova->id = *music_id;
strcpy(nova->tittle, "Bom Senso");
strcpy(nova->artist, "Tim Maia");
strcpy(nova->album, "Racional");
nova->duration = 155;
inserir_music(source, nova);
}
int main_menu() {
int op;
printf("\n---------------------------------------------\n"
" OPTIONS MENU"
"\n---------------------------------------------\n"
"1 - Music menu\n"
"2 - Playlist menu\n"
"3 - Exit\n\n"
"Escolha uma opcoes: ");
fflush(stdin);
scanf("%d", &op);
return op;
}
int music_menu() {
int op;
printf("\n---------------------------------------------\n"
" MUSIC MENU"
"\n---------------------------------------------\n"
"1 - Insert music\n"
"2 - Delete music\n"
"3 - Print musics\n"
"4 - Back\n\n"
"Escolha uma opcoes: ");
scanf("%d", &op);
return op;
}
int menuPlaylist() {
int op;
printf("\n\n---------------------------------------------\n"
" PLAYLIST MENU"
"\n---------------------------------------------\n"
"1 - Insert playlist\n"
"2 - Print playlists\n"
"3 - Shuffle\n"
"4 - Back\n\n"
"Choose a option: ");
scanf("%d", &op);
return op;
}
//Main Application
int main(){
int op = 0;
int music_contactor = 0;
int playlist_contactor = 0;
int music_id = 0;
int playlist_id = 0;
music music_node;
tree_AVL *source = build_tree_node();
playlist_node start_playlist;
lplaylist_node start_playlist_list;
start_playlist_list = head_playlist_list();
create_test_musics(&music_contactor, &(*source), &music_id);
while (op != 3) {
int op = main_menu();
if (op == 1) {
int option = 0;
while (option != 4) {
option = music_menu();
if (option == 1) {
register_music(&music_contactor, source, &music_id);
printf("\nMusic successfully registered!\n");
} else
if (option == 2) {
if(music_contactor == 0){
printf("There are no musics to be deleted\n");
}else{
int id;
print_musics_id_name(source);
printf("\nWhich music do you want to delete: ");
scanf("%d", &id);
delete_music_playlist(id, &start_playlist_list);
int res = delete_music(source, id, &music_contactor);
}
}if (option == 3) {
if(music_contactor == 0){
printf("There are no musics to be shown\n");
}else{
print_musics(source);
}
}
}
} else
if (op == 2) {
lplaylist_node p;
int option = 0;
while (option != 4) {
option = menuPlaylist();
if (option == 1) {
print_musics_id_name(source);
selected_music(source, &start_playlist_list, &playlist_contactor, &playlist_id);
printf("\nPlaylist successfully registered!\n");
}else if (option == 2) {
if(playlist_contactor == 0){
printf("There is no playlist registered\n");
}else{
print_name_playlists(&start_playlist_list);
p = search_playlist(&start_playlist_list);
print_musics_playlist(&p);
}
} else if(option == 3){
if(playlist_contactor == 0){
printf("There is no playlist registered\n");
}else{
print_name_playlists(&start_playlist_list);
p = search_playlist(&start_playlist_list);
function_shuffle(&p);
}
}
}
}
else {
break;
}
}
}