I'm trying to make a vector hash with offset for treatment of collisions. My program is giving a mistake. Can someone help me find the reason for the error?
Follow 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;
}