-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodigo.C
354 lines (283 loc) · 8.67 KB
/
Codigo.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 53
typedef struct Node {
char nome[100];
struct Node* anterior;
struct Node* proximo;
} Node;
unsigned int funcaoHash(const char* chave);
void inserir(const char* nome);
int busca(const char* nome);
int contarElementos(const unsigned int chave);
void contarElementosEnomes(const unsigned int chave, int* contar, char** nomes);
unsigned int removerNome(const char* nome);
Node* particao(Node* start, Node* end);
void quicksort(Node* start, Node* end);
void troca(Node* a, Node* b);
void print_tabela_hash();
void limpar_tabela_hash();
Node* tabela_hash[TABLE_SIZE];
unsigned int funcaoHash(const char* chave) {
unsigned int hash = 0;
unsigned int len = strlen(chave);
for (unsigned int i = 0; i < len; i++) {
hash = hash * 31 + chave[i];
}
return hash % TABLE_SIZE;
}
void inserir(const char* nome) {
unsigned int chave = funcaoHash(nome);
Node* novo_node = (Node*)malloc(sizeof(Node));
strcpy(novo_node->nome, nome);
novo_node->anterior = NULL;
novo_node->proximo = NULL;
if (tabela_hash[chave] == NULL) {
tabela_hash[chave] = novo_node;
} else {
novo_node->proximo = tabela_hash[chave];
tabela_hash[chave]->anterior = novo_node;
tabela_hash[chave] = novo_node;
}
}
int busca(const char* nome) {
unsigned int chave = funcaoHash(nome);
Node* atual = tabela_hash[chave];
while (atual != NULL) {
if (strcmp(atual->nome, nome) == 0) {
return 1;
}
atual = atual->proximo;
}
return 0;
}
int contarElementos(const unsigned int chave) {
int contar = 0;
Node* atual = tabela_hash[chave];
while (atual != NULL) {
contar++;
atual = atual->proximo;
}
return contar;
}
void contarElementosEnomes(const unsigned int chave, int* contar, char** nomes) {
*contar = 0;
Node* atual = tabela_hash[chave];
while (atual != NULL) {
(*contar)++;
atual = atual->proximo;
}
*nomes = (char**)malloc((*contar) * sizeof(char*));
atual = tabela_hash[chave];
int i = 0;
while (atual != NULL) {
(*nomes)[i] = strdup(atual->nome);
atual = atual->proximo;
i++;
}
}
unsigned int removerNome(const char* nome) {
unsigned int chave = funcaoHash(nome);
Node* atual = tabela_hash[chave];
while (atual != NULL) {
if (strcmp(atual->nome, nome) == 0) {
if (atual->anterior == NULL) {
tabela_hash[chave] = atual->proximo;
if (atual->proximo != NULL) {
atual->proximo->anterior = NULL;
}
} else {
atual->anterior->proximo = atual->proximo;
if (atual->proximo != NULL) {
atual->proximo->anterior = atual->anterior;
}
}
free(atual);
return chave;
}
atual = atual->proximo;
}
return -1;
}
void troca(Node* a, Node* b) {
char temp[100];
strcpy(temp, a->nome);
strcpy(a->nome, b->nome);
strcpy(b->nome, temp);
}
Node* particao(Node* start, Node* end) {
char pivot[100];
strcpy(pivot, end->nome);
Node* i = start->anterior;
for (Node* j = start; j != end; j = j->proximo) {
if (strcmp(j->nome, pivot) <= 0) {
i = (i == NULL) ? start : i->proximo;
if (i != j) {
troca(i, j);
}
}
}
i = (i == NULL) ? start : i->proximo;
if (i != end) {
troca(i, end);
}
return i;
}
void quicksort(Node* start, Node* end) {
if (start && end && start != end && start != end->proximo) {
Node* pivot = particao(start, end);
quicksort(start, pivot->anterior);
quicksort(pivot->proximo, end);
}
}
void sort_tabela_hash() {
for (int i = 0; i < TABLE_SIZE; i++) {
Node* head = tabela_hash[i];
Node* tail = NULL;
while (head && head->proximo) {
head = head->proximo;
}
tail = head;
quicksort(tabela_hash[i], tail);
}
}
void print_tabela_hash() {
printf("Histograma:\n");
printf("-----------\n");
for (int i = 0; i < TABLE_SIZE; i++) {
printf("Chave [%d]: %d elementos\n", i, contarElementos(i));
}
}
void limpar_tabela_hash() {
for (int i = 0; i < TABLE_SIZE; i++) {
Node* atual = tabela_hash[i];
while (atual != NULL) {
Node* proximo = atual->proximo;
free(atual);
atual = proximo;
}
tabela_hash[i] = NULL;
}
}
int comparar_nomes(const void* a, const void* b) {
const char* nome1 = *(const char**)a;
const char* nome2 = *(const char**)b;
return strcasecmp(nome1, nome2);
}
void print_all_nomes_sorted() {
const int MAX_NAMES = 100788;
char* all_nomes[MAX_NAMES];
int contar = 0;
for (int i = 0; i < TABLE_SIZE; i++) {
Node* atual = tabela_hash[i];
while (atual != NULL && contar < MAX_NAMES) {
all_nomes[contar] = strdup(atual->nome);
contar++;
atual = atual->proximo;
}
}
qsort(all_nomes, contar, sizeof(char*), comparar_nomes);
printf("Nomes ordenados:\n");
printf("----------------\n");
for (int i = 0; i < contar; i++) {
printf("%s\n", all_nomes[i]);
free(all_nomes[i]);
}
memset(all_nomes, 0, sizeof(all_nomes));
}
int main() {
for (int i = 0; i < TABLE_SIZE; i++) {
tabela_hash[i] = NULL;
}
FILE* file = fopen("nomes.txt", "r");
if (file == NULL) {
printf("Erro ao abrir o arquivo.\n");
return 1;
}
char nome[100];
while (fgets(nome, sizeof(nome), file)) {
nome[strcspn(nome, "\r\n")] = '\0';
inserir(nome);
}
fclose(file);
int escolha;
do {
printf("Menu:\n");
printf("1. Inserir nome\n");
printf("2. Consultar nome\n");
printf("3. Verificar quantidade de elementos por chave\n");
printf("4. Remover nome\n");
printf("5. Imprimir chaves\n");
printf("6. Ordenar elementos\n");
printf("7. Imprimir todos os nomes de todas as chaves \n");
printf("8. Sair\n");
printf("Escolha uma opção: ");
scanf("%d", &escolha);
switch (escolha) {
case 1:
printf("Digite o nome a ser inserido: ");
scanf(" %[^\n]", nome);
inserir(nome);
printf("Nome inserido com sucesso na chave %d.\n", funcaoHash(nome));
break;
case 2: {
printf("Digite o nome a ser consultado: ");
scanf(" %[^\n]", nome);
int chave = funcaoHash(nome);
int found = busca(nome);
if (found) {
printf("Consulta: \"%s\" encontrado na chave %d.\n",
nome, chave);
} else {
printf("Consulta: \"%s\" não encontrado.\n", nome);
}
break;
}
case 3: {
printf("Digite a chave a ser verificada: ");
scanf("%d", &escolha);
int contar = contarElementos(escolha);
printf("Elementos na chave %d:\n", escolha);
Node* atual = tabela_hash[escolha];
while (atual != NULL) {
printf("%s\n", atual->nome);
atual = atual->proximo;
}
printf("Chave [%d]: %d elementos\n", escolha, contar);
break;
}
case 4: {
printf("Digite o nome a ser removido: ");
scanf(" %[^\n]", nome);
unsigned int chave = removerNome(nome);
if (chave == -1) {
printf("O nome \"%s\" não foi encontrado.\n", nome);
} else {
printf("Nome \"%s\" removido com sucesso da chave %d.\n", nome, chave);
}
break;
}
case 5:
print_tabela_hash();
break;
case 6: {
sort_tabela_hash();
printf("Elementos em cada chave ordenados.\n");
break;
}
case 7:
print_all_nomes_sorted();
break;
case 8:
printf("Encerrando o programa.\n");
break;
default:
printf("Opção inválida. Tente novamente.\n");
break;
}
printf("\n");
} while (escolha != 8);
limpar_tabela_hash();
return 0;
}