-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
116 lines (100 loc) · 2.84 KB
/
sort.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
#include "sort.h"
char artistCmp(Song * song1, Song * song2){
return strdiff(song1->artistname, song2->artistname);
}
char dateAddedCmp(Song * song1, Song * song2){
return !momentcmp(song1->date_added, song2->date_added);
}
char dateReleaseCmp(Song * song1, Song * song2){
return momentcmp(song1->date_release, song2->date_release);
}
char decreasenameCmp(Song * song1, Song * song2){
return !strdiff(song1->name, song2->name);
}
int findIndexDLList(DLinkedList * list, DNode * node, char type){
cmpFunction * cmp = getTypeSortDLList(type);
if(cmp != NULL){
return linearSearchDLList(list, node, cmp);
}
return -1;
}
char genreCmp(Song * song1, Song * song2){
return strdiff(song1->genre, song2->genre);
}
cmpFunction * getTypeSortDLList(char type){
switch (type){
case 0: // Data de adicao
return dateAddedCmp;
case 1: // Data de lancamento
return dateReleaseCmp;
case 2: // Alfabetica - Nome (Crescente)
return nameCmp;
case 3: // Alfabetica - Nome (Decrescente)
return decreasenameCmp;
case 4: // Alfabetica - Artista/Banda
return artistCmp;
case 5: // Alfabetica - Genero
return genreCmp;
default:
return NULL;
}
}
int linearSearchDLList(DLinkedList * list, DNode * node, cmpFunction * cmp){
DNode * current = list->head;
int index = 0;
while(current != NULL && cmp(current->song, node->song) == 1){
current = current->next;
index++;
}
return index;
}
DLinkedList * mergeSort(DLinkedList * list, cmpFunction * cmp){
if (list->length == 1) return list;
DLinkedList *list1, *list2;
int half = list->length / 2;
list1 = createDLList();
list2 = createDLList();
splitDLList(list, list1, list2, half);
return mergeDLList(mergeSort(list1, cmp), mergeSort(list2, cmp), cmp);
}
DLinkedList * mergeDLList(DLinkedList * first, DLinkedList * second, cmpFunction * cmp){
DNode * currentFirst = first->head;
DNode * currentSecond = second->head;
DLinkedList * mergedList = createDLList();
while (currentFirst != NULL || currentSecond != NULL){
if (currentFirst == NULL ) {
unshiftDLList(mergedList, currentSecond);
currentSecond = currentSecond->next;
continue;
}
if (currentSecond == NULL ) {
unshiftDLList(mergedList, currentFirst);
currentFirst = currentFirst->next;
continue;
}
if (cmp(currentFirst->song, currentSecond->song) == 1) {
unshiftDLList(mergedList, currentFirst);
currentFirst = currentFirst->next;
} else {
unshiftDLList(mergedList, currentSecond);
currentSecond = currentSecond->next;
}
}
return mergedList;
}
char nameCmp(Song * song1, Song * song2){
return strdiff(song1->name, song2->name);
}
DLinkedList * sortDLList(DLinkedList * list, char type){
cmpFunction * cmp = getTypeSortDLList(type);
if(cmp != NULL){
return mergeSort(list, cmp);
}
return NULL;
}
char strdiff(char * name1, char * name2){
if(strcmp(name1, name2) > 0){
return 0;
}
return 1;
}