0
I am trying to hash vectors with displacement for collision treatment. My program is giving an error. Someone can help me find the reason for the mistake?
Follows the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
int hash (int c, int M){
return c%M;
}
int deslocamento(int c){
return (c%2) + 1;
}
void inserir(int chave, int pos, int hash_table[], int M){
if(hash_table[pos] == -1){
hash_table[pos] = chave;
}else{
int novaposicao = pos + deslocamento(chave);
while(novaposicao > 7){
novaposicao = pos + deslocamento(chave);
}
hash_table[novaposicao] = chave;
}
}
void imprimeTabelaEncadeada(int tabela[], int m){
int i;
for(i = 0; i < m; i++){
if((tabela[i] == -1)){
printf("Vazio");
}else{
printf("Chave: %d", tabela[i]);
}
printf("\n");
}
}
int main(){
int M = 7;
int hash_table[7];
int i;
for (i = 0 ; i < M; i++){
hash_table[i] = -1;
}
for (i = 0 ; i < M; i++){
printf("%d", hash_table[i]);
}
printf("\n");
int N = 5;
int chaves[] = {24,76,39,61,25};
int pos;
for (i = 0; i < N; i++){
pos = hash(chaves[i], M);
printf("%d\n", pos);
inserir(chaves[i], pos, hash_table, M);
}
imprimeTabelaEncadeada(hash_table, M);
return 0;
}
That function
deslocamento
always returns 1 or 2. Is this right? If so, why?– Victor Stafusa
So this function displacement is something that had in question, I think it would be easier to look for an empty value in the vector and assign in it my new value. But from what I understood it was for this function to displace the vector items in case there was conflict.
– Israel Jesus Filho
Couldn’t you provide more information ? "I’m a doctor Jim, not a Telepath"
– zentrunix