-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenreHash.h
131 lines (110 loc) · 2.46 KB
/
GenreHash.h
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
class hashNode {
public:
hashNode* next;
string song;
string artist;
string gen;
hashNode() {
next = NULL;
artist = "";
gen = "";
song = "";
}
};
class hashTable {
public:
const int NumberofGenres = 14;
hashNode** hashtable;
hashNode* loc;
hashNode* ploc;
string genre[14] = { "rock","pop","adult standards","hip hop","metal","wave","mellow","british","disco","dutch","country","classic","electro","folk" };
int count[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
hashTable() {
hashtable = new hashNode * [NumberofGenres];
loc = NULL;
ploc = NULL;
for (int i = 0; i < NumberofGenres; i++) {
hashtable[i] = NULL;
}
}
int hashFunction(string gen) {
int i;
for (i= 0; i < NumberofGenres; i++) {
if (gen == genre[i]) {
break;
}
}
return i;
}
void Search(string gen, string artist) {
int pos = hashFunction(gen);
ploc = NULL;
loc = hashtable[pos];
while (loc != NULL and loc->artist < artist) {
ploc = loc;
loc = loc->next;
}
if (loc != NULL and loc->artist != artist) {
loc = NULL;
}
}
void InsertSorted(string gen, string artist,string song) {
Search(gen, artist);
if (loc == NULL) {
int pos = hashFunction(gen);
count[pos] = count[pos] + 1;
hashNode* newnode = new hashNode();
newnode->artist = artist;
newnode->next = NULL;
newnode->song = song;
if (hashtable[pos] == NULL) {
hashtable[pos] = newnode;
}
else {
if (ploc == NULL) {
//insert at front
newnode->next = hashtable[pos];
hashtable[pos] = newnode;
}
else if (ploc->next == NULL) {
ploc->next = newnode;
}
else {
newnode->next = ploc->next;
ploc->next = newnode;
}
}
}
else {
//do nothing
//node already exists
}
}
void print(string gen) {
int j = hashFunction(gen);
hashNode* curr = hashtable[j];
cout << genre[j] << " [" << count[j] << "] " << ": ";
while (curr != NULL) {
if (curr->next == NULL) {
cout << curr->song;
break;
}
cout << curr->song << "-->";
curr = curr->next;
}
cout << endl;
cout << endl;
}
int gibrandom(int num) {
return rand() % (count[num] + 1 - 0) + 0;
}
string getstring(string gen) {
int pos = hashFunction(gen);
int ran = gibrandom(pos);
loc = hashtable[pos];
for (int i = 0; i < ran; i++) {
loc = loc->next;
}
return loc->artist;
}
};