Olá,
Fiz uma Tabela Hash com TAD Lista, onde armazeno contatos com nome, cidade e cpf, porém a função de Busca por cpf não encontra sua conta alvo, e eu não consegui achar o erro até agora.
Aqui esta o .cpp da Lista:
<CODE>
#include "lista.hpp"
#include <bits/stdc++.h>
using namespace std;
void criaHash(Hash *hash,int num1,int num2) {
int i;
hash->n = 0;
hash->num_listas = num1;
hash->num_pesos = num2;
hash->vetor_listas = new Lista[num1];
for(i=0; i<num1; i++) {
iniciaLista(&hash->vetor_listas); } hash->vetor_pesos = new int [num2]; for(i=0; i<num2; i++) { hash->vetor_pesos = rand() % 1000; } } bool insereHash(Hash *hash,Item *x) { if(Hash_PesquisaCelula(hash,x->chave)==NULL) { insereLista(&hash->vetor_listas[Hash_H(hash,x->chave)],x); hash->n++; return true; } return false; } bool listaVazia(Lista* lista) { if(lista->primeiro == NULL) return true; else return false; } void iniciaLista(Lista* lista) { lista -> primeiro = new Celula; lista -> ultimo = lista -> primeiro; lista -> primeiro -> prox = NULL; } void insereLista(Lista* lista,Item* x) { lista -> ultimo->prox = new Celula; lista -> ultimo = lista -> ultimo->prox; lista -> ultimo->item.registro_hash.nome = x->registro_hash.nome; lista -> ultimo->item.registro_hash.cpf = x->registro_hash.cpf; lista -> ultimo->item.registro_hash.cidade = x->registro_hash.cidade; lista -> ultimo->prox = NULL; } int Hash_H(Hash *hash,int *chave) { int soma=0; for(int i=0; i<11; i++) { soma += chave * hash->vetor_pesos[i%hash->num_pesos]; } return (soma % hash->num_listas); } void Hash_Pesquisa(Hash *hash,int *chave,Item *x) { Celula *aux = Hash_PesquisaCelula(hash,chave); if(aux == NULL) { cout <<"CPF: "; for(int i=0; i<11; i++) { cout<<chave; } cout<<" Nao encontrado"<<endl; } else { *x = aux->prox->item; cout<<"Nome: "<<aux->prox->item.registro_hash.nome<<endl; cout <<"CPF: "<<aux->prox->item.registro_hash.cpf<<endl; cout <<"Cidade: "<<aux->prox->item.registro_hash.cidade<<endl; } } Celula* Hash_PesquisaCelula(Hash *hash,int *chave) { int i = Hash_H(hash,chave); Celula *aux = hash->vetor_listas.primeiro; if(listaVazia(&hash->vetor_listas)) { return NULL; } while (aux->prox != NULL) { if(chave==aux->item.chave) return aux; aux = aux->prox; } if(chave==aux->item.chave) return aux; else return NULL; }
[fim do codigo]