olá pessoal , pois tenho que fazer um trabalho em c++ pela tabela hash seria o seguinte :
1-inserir nomes e telefones de uma pessoa,
2- depois remover o nome e o telefone e
3- E consultar o telefone .
alguém poderia me ajudar por favor. o que consegui fazer foi isso :
eu quero agora implementar o telefone , mais não sei fazer? se alguém souber me ajude por favor!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAMTAB 37
typedef char string[30];
typedef struct{
string tab[TAMTAB];
int ocup;
}tabela;
void pausa(){
fflush(stdin);
getchar();
}
int hash(char *nom){
int soma, i;
soma = 0;
for(i = 0; i < strlen(nom); i++)
soma += (i+1)*nom[i];
soma = soma%TAMTAB;
if(soma == 0)
return 1;
return soma;
}
int hash_insere(tabela *t, char *nome, int h){
if(strcmp(t->tab[h],"\0") == 0 || strcmp(t->tab[h], "*") == 0){
strcpy(t->tab[h], nome);
t->ocup++;
printf("\n\tO nome foi inserido na posicao %d\n", h);
return 0;
}
return 1;
}
int hash_procura(tabela *t, char *nome, int h){
int i = 0;
while(strcmp(t->tab[(h*++i*i)%TAMTAB],"\0") != 0 && strcmp(t->tab[(h*i*i)%TAMTAB], nome) != 0); //Faz ateh achar um espaco vazio ou achar o nome.
if(strcmp(t->tab[(h*i*i)%TAMTAB],"\0") == 0){
return -1;//Se nao achou, devolve -1
}
else{
printf("\n\tO nome foi encontrado na posição %d\n", h*i*i%TAMTAB);
return (h*i*i)%TAMTAB; //se achou, em que posicao achou?
}
}
void hash_remover(tabela *t, char *nome){
int a;
a = hash_procura(t, nome, hash(nome));
if(a < 0)
printf("\n\tO nome %s procurado nao consta na tabela.\n", nome);
else{
strcpy(t->tab[a], "*");
t->ocup--;
printf("\n\tO nome foi removido: %s\n", nome);
}
}
void menu(){
printf("\t0. Sair do programa\n");
printf("\t1. Inserir Nomes;\n");
printf("\t2. Procurar Nomes;\n");
printf("\t3. Remover Nomes;\n");
printf("\n\nSua escolha: ");
}
void escolha(tabela *t){
string nome;
int i, h, opc;
scanf("%d", &opc);
switch(opc){
case 0:
break;
case 1:
printf("\nDigite os nomes a serem colocados na tabela.\n");
printf("Termine a sequencia com \"*\":\n");
do{
scanf("%s", nome);
if(strcmp(nome, "*") != 0){
i = 0;
h = hash(nome);
while(hash_insere(t, nome, ((++i*h*i)%TAMTAB)));
//printf("i = %d\n", i); //imprimir o numero de tentativas
}
}while(strcmp(nome, "*")); //leitura
break;
case 2:
printf("\nDigite os nomes a serem consultados.\n");
printf("Termine a sequencia com \"*\"\n");
do{
scanf("%s", nome);
if(strcmp(nome, "*") != 0){
h = hash(nome);
i = hash_procura(t, nome,h);
if(i < 0){
printf("\n\tO nome %s nao foi encontrado\n");
}
else{
printf("\n\t%s esta na tabela!\n");
}
}
}while(strcmp(nome, "*"));//procura
break;
case 3:
printf("\nDigite os nomes a serem removidos.\n");
printf("Termine a sequencia com \"*\":\n");
do{
scanf("%s", nome);
if(strcmp(nome, "*") != 0){
hash_remover(t, nome);
}
}while(strcmp(nome, "*"));//remocao
break;
default:
printf("Opcao invalida. Por favor, digite uma opcao valida.\n");
break;
}
if(opc != 0){
menu();
escolha(t);
}
}
int main(){
tabela t;
int i;
for(i = 0; i < TAMTAB; i++){
strcpy(t.tab[i],"\0");
}
t.ocup = 0;//Ateh aqui, iniciar tabela.
menu();
escolha(&t);
printf("Saindo do programa....\n");
}