-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashEncadeada.c
209 lines (184 loc) · 6.44 KB
/
hashEncadeada.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
/*=======================================================================*/
/* Trabalho de AED II - Indice remissivo com árvores */
/* Nome: Arthur Alexsander Martins Teodoro */
/* MAtrícula: 0022427 Data: 23/11/2016 */
/*=======================================================================*/
/*=======================================================================*/
/* BIBLIOTECAS USADAS */
/*=======================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "hashEncadeada.h"
#include "listadup.h"
/*=======================================================================*/
/* ESTRUTURA CRIADA */
/*=======================================================================*/
struct palavra
{
char plv[33];
Lista ocorrencias;
struct palavra *prox;
};
struct hash
{
int tam;
int colisao;
struct palavra **vetor;
};
/*=======================================================================*/
/*FUNCAO HASH - FUNCAO RESPONSAVEL POR DEFINIR POSICAO */
/*IN: PALAVRA OUT: POSICAO */
/*=======================================================================*/
int funcaoHash(char* palavra, int tam)
{
unsigned int ascii = 0;
int i;
for(i = 1; i <= strlen(palavra); i++)
{
ascii = ascii + (palavra[i-1] * i);
}
return ascii % tam;
}
/*=======================================================================*/
/*CRIA HASH - FUNCAO DE CRIACAO DA HASH */
/*IN: VOID OUT: PONTEIRO PARA HASH */
/*=======================================================================*/
Hash criaHash(int tam)
{
int i;
srand(time(NULL));
Hash hash = (Hash) malloc(sizeof(struct hash));
hash->tam = tam;
hash->colisao = 0;
hash->vetor = (Palavra*) malloc(sizeof(Palavra)*tam);
for(i = 0; i < tam; i++)
{
hash->vetor[i] = NULL;
}
return hash;
}
/*=======================================================================*/
/*DESTROI HASH - FUNCAO DE DESTRUICAO DA HASH */
/*IN: PONTEIRO PARA HASH OUT: VOID */
/*=======================================================================*/
void destroiHash(Hash hash)
{
Palavra excluir;
int i;
for(i = 0; i < hash->tam; i++)
{
while(hash->vetor[i] != NULL)
{
excluir = hash->vetor[i];
hash->vetor[i] = excluir->prox;
destroiLista(excluir->ocorrencias);
free(excluir);
}
}
free(hash->vetor);
free(hash);
hash = NULL;
return;
}
/*=======================================================================*/
/*INSERE HASH - FUNCAO RESPONSAVEL POR INSERIR PALAVRA */
/*IN: HASH E PALAVRA A INSERIR OUT: PONTEIRO PARA PALAVRA */
/*=======================================================================*/
Palavra insereHash(Hash hash, char* palavra)
{
int posicao = funcaoHash(palavra, hash->tam);
Palavra inserir = (Palavra) malloc(sizeof(struct palavra));
if(hash->vetor[posicao] != NULL)
hash->colisao++;
inserir->prox = hash->vetor[posicao];
hash->vetor[posicao] = inserir;
strcpy(inserir->plv, palavra);
inserir->ocorrencias = criaLista();
return inserir;
}
/*=======================================================================*/
/*BUSCA HASH - FUNCAO DE BUSCA DA HASH */
/*IN: HASH E PALAVRA A BUSCAR OUT: PONTEIRO PARA PALAVRA */
/*=======================================================================*/
Palavra buscaHash(Hash hash, char* palavra)
{
int posicao = funcaoHash(palavra, hash->tam);
Palavra olhar = hash->vetor[posicao];
while(olhar != NULL && strcmp(palavra, olhar->plv))
{
olhar = olhar->prox;
}
if(olhar == NULL)
{
return NULL;
}
return olhar;
}
/*=======================================================================*/
/*INSERE OCORRENCIA - FUNCAO RESPONSAVEL POR INSERIR LINHA */
/*IN: HASH, PALAVRA E LINHA OUT: VOID */
/*=======================================================================*/
void insereOcorrenciaHash(Hash hash, char* palavra, int linha)
{
Palavra plv = buscaHash(hash, palavra);
if(plv != NULL)
{
if(!existeLista(plv->ocorrencias, linha))
insereLista(plv->ocorrencias, tamanhoLista(plv->ocorrencias), linha);
}
}
/*=======================================================================*/
/*TAMANHO HASH - FUNCAO RESPONSAVEL POR RETORNAR TAMANHO HASH */
/*IN: HASH OUT: INT */
/*=======================================================================*/
int tamanhoHash(Hash hash)
{
return hash->tam;
}
/*=======================================================================*/
/*RETORNA PALAVRA - FUNCAO QUE RETORNA A STRING DA STRUCT */
/*IN: PALAVRA OUT: CHAR* */
/*=======================================================================*/
char* retornaPalavraHash(Palavra plv)
{
return plv->plv;
}
/*=======================================================================*/
/*RETORNA LISTA - FUNCAO QUE RETORNA A LISTA DA STRUCT */
/*IN: PALAVRA OUT: LISTA */
/*=======================================================================*/
Lista retornaListaHash(Palavra plv)
{
return plv->ocorrencias;
}
/*=======================================================================*/
/*PRINTA HASH - FUNCAO QUE IMPRIME A HASH */
/*IN: PALAVRA OUT: VOID */
/*=======================================================================*/
void printaHash(Hash hash)
{
int i;
Palavra plv;
for(i = 0; i < hash->tam; i++)
{
if(hash->vetor[i] != NULL)
{
plv = hash->vetor[i];
while(plv != NULL)
{
printf("%s\n", plv->plv);
plv = plv->prox;
}
}
}
}
/*=======================================================================*/
/*RETORNA COLISAO - FUNCAO QUE RETORNA QUANT DE COLISAO */
/*IN: HASH OUT: INT */
/*=======================================================================*/
int colisaoHash(Hash hash)
{
return hash->colisao;
}