Last active
March 1, 2020 14:32
-
-
Save divanibarbosa/31a53f604d2435a5fc09fba77e6eb92d to your computer and use it in GitHub Desktop.
Hash Table with Linear Colisions - Tabela HASH com tratamento de colisões LINEAR em C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tabela HASH com tratamento de colisões LINEAR em C | |
// profa. Divani Barbosa | |
// Curriculo Lattes: http://lattes.cnpq.br/8503400830635447 | |
// Ultima atualização 18/07/2017 | |
// divanibarbosa@gmail.com | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define TAM_MAX 7 | |
struct reg { | |
float item; | |
int ocupado; | |
}; | |
typedef struct reg HASH; | |
int funcaohash(float); | |
void inicializa(HASH []); | |
void insere(HASH [], float); | |
int busca(HASH [], float); | |
void apaga(HASH [], float); | |
void imprime(HASH []); | |
int cheia(HASH []); | |
main() { | |
int i, pos; | |
HASH tab[TAM_MAX]; // criando tabela na memoria | |
float item; | |
inicializa(tab); | |
// Inserindo elementos (USUARIO) | |
printf("\n*************************************************************\n"); | |
printf("Tabela HASH com tratamento de colisoes linear (7 itens reais)"); | |
printf("\n*************************************************************"); | |
for (i=0; i<TAM_MAX; i++){ | |
printf("\n\nInserindo elemento %d",i+1); | |
printf(" - Forneca valor real: "); | |
scanf("%f",&item); | |
insere(tab,item); | |
} | |
printf("\n\nBuscando campo\n>>> Forneca o item: "); | |
scanf("%f",&item); | |
pos=busca(tab,item); | |
if (pos!=-1) | |
printf("Item %.2f encontrado na posicao %d", tab[pos].item,pos); | |
else printf("Item nao encontrado"); | |
printf("\n\nApagando campo\n>>> Forneca o item: "); | |
scanf("%f",&item); | |
apaga(tab,item); | |
printf("\n\nImprimindo conteudo"); | |
imprime(tab); | |
puts("\n"); | |
system("pause"); | |
} | |
int funcaohash(float chave) { | |
return abs(chave) % TAM_MAX; | |
} | |
void inicializa(HASH tabHash[]) { | |
int i; | |
for(i=0; i<TAM_MAX; i++) | |
tabHash[i].ocupado = 0; // falso | |
} | |
void insere(HASH tabHash[], float e) { | |
if (cheia(tabHash)) { // Se tabela cheia, imprime aviso e sai da função | |
puts("\n->ATENCAO Tabela cheia"); | |
return; // saida imediata da função, nao executa os comandos abaixo | |
} | |
int pos = funcaohash(e); // CALCULA POSIÇAO | |
// INICIO ROTINA TRATAMENTO DE COLISAO | |
if (tabHash[pos].ocupado == 1) { // se ocorreu colisao | |
if (e==tabHash[pos].item) { // se a chave ja existe | |
puts("\n->ATENCAO Esse item ja foi cadastrado"); | |
return; // saida imediata da função | |
} | |
printf("-> Ocorreu uma colisao na posicao %d\n",pos); | |
while (pos < TAM_MAX) { | |
if (pos==TAM_MAX-1) pos=-1; // volta para o inicio da tabela | |
pos++; // incremento mais um no indice | |
if (tabHash[pos].ocupado == 0) // se a posição estiver livre | |
break; // sai do loop com o indice na posicao correta (pos sem colisão) | |
} | |
} | |
// FIM ROTINA TRATAMENTO DE COLISAO | |
tabHash[pos].item=e; | |
tabHash[pos].ocupado = 1; | |
printf("-> Inserido HASH[%d]",pos); | |
} | |
// Retorna -1 se nao encontrou ou a posição caso encontre o item | |
int busca(HASH tabHash[], float chave) { // Recuperando um elemento | |
int pos = funcaohash(chave); | |
int iniciobusca = pos; | |
if (tabHash[pos].ocupado == 1) { // se nessa posicao esta ocupado | |
if (tabHash[pos].item == chave) // Se item e chave são iguais | |
return pos; // saida imediata da função | |
// INICIO ROTINA TRATAMENTO DE COLISAO (se o item e chave nao sao iguais) | |
while (pos < TAM_MAX) { // percorre proximas posições do vetor | |
if (pos==TAM_MAX-1) pos=-1; // volta para o inicio da tabela | |
pos++; // incremento mais um no indice | |
if (tabHash[pos].ocupado == 1 && tabHash[pos].item == chave) | |
// se o campo esta ocupado e o item foi encontrado | |
return pos; // saida imediata da função | |
if (pos==iniciobusca) return -1; // se percorreu tudo e nao encontrou | |
} | |
// FIM ROTINA TRATAMENTO DE COLISAO | |
} | |
return -1; // Se nao existe essa chave | |
} | |
void apaga(HASH tabHash[], float chave) { | |
int pos = busca(tabHash,chave); | |
if (pos!=-1) { | |
tabHash[pos].ocupado=0; | |
printf("-> Dados HASH[%d] apagados",pos); | |
} | |
else printf("Item nao encontrado"); | |
} | |
void imprime(HASH tabHash[]) { | |
int i; | |
for (i=0; i<TAM_MAX; i++) | |
if( tabHash[i].ocupado == 1) | |
printf("\nCampo [%d] = %.2f",i,tabHash[i].item); | |
} | |
int cheia(HASH tabHash[]) { | |
int i, qtde=0; | |
for (i=0; i<TAM_MAX; i++) | |
if( tabHash[i].ocupado == 1) | |
qtde++; | |
if (qtde == TAM_MAX) return 1; | |
else return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment