Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "hash.h"
- struct hash{
- int qtd,TABLE_SIZE;
- city **itens;
- };
- int vet[854],k=0;
- int criptografia(char c[]){
- int x=0,i;
- for(i=0;i<strlen(c);i++)x+=31*(int)c[i];
- return x;
- }
- void ler(city ci[]){
- FILE *arq;
- arq = fopen("cidades1.txt","r");
- char c;
- int i=0,k=0;
- while((!feof(arq))){
- fscanf(arq,"%c",&c);
- if(c=='\n'){
- ci[k].c[i]='\0';
- k++;
- i=0;
- }
- else{
- ci[k].c[i]=c;
- i++;
- }
- }
- ci[k].c[i]=c;
- ci[k].c[i]='\0';
- fclose(arq);
- }
- int chaveDivisao(int chave, int TABLE_SIZE){
- return (chave&0x7FFFFFFF) % TABLE_SIZE;
- }
- int chaveDobra(int chave, int TABLE_SIZE){
- int num_bits = 10;
- int parte1 = chave >> num_bits;
- int parte2 = chave & (TABLE_SIZE-1);
- return (parte1 ^ parte2);
- }
- int insereHash(Hash *ha,city cidade[]){
- if(ha == NULL || ha->qtd == ha->TABLE_SIZE){
- return 0;
- }
- int chave = criptografia(cidade[k].c); //MUDAR AQUI CASO NÃO SEJA DESSA FORMA: int chave = a1.matricula
- k++;
- int pos = chaveDivisao(chave,ha->TABLE_SIZE);
- city *novo;
- novo = (city*)malloc(sizeof(city));
- if(novo == NULL){
- return 0;
- }
- novo = cidade; //No do backes *novo = al
- ha->itens[pos] = novo;
- ha->qtd++;
- return 1;
- }
- void inserir(city ci[]){
- Hash *ha=criaHash(10);
- insereHash(ha,ci);
- }
- Hash* criaHash(int TABLE_SIZE){
- Hash* ha =(Hash*) malloc(sizeof(Hash));
- if(ha!=NULL){
- int i;
- ha->TABLE_SIZE=TABLE_SIZE;
- ha->itens=(city**)malloc(TABLE_SIZE*sizeof(city*));
- if(ha->itens==NULL){
- free(ha);
- return NULL;
- }
- ha->qtd=0;
- for(i=0;i<ha->TABLE_SIZE;i++){
- ha->itens[i]=NULL;
- }
- }
- return ha;
- }
- void liberaHash(Hash *ha){
- if(ha != NULL){
- int i;
- for(i=0; i < ha->TABLE_SIZE; i++){
- if(ha->itens[i] != NULL){
- free(ha->itens[i]);
- }
- }
- free(ha->itens);
- free(ha);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement