Sunteți pe pagina 1din 3

D:\Descargas\baangaritar-mtvelasquezg\baangaritar-mtvelasquezg\etc\hashtable.h jueves, 28 de abril de 2016 8:47 p. m.

#ifndef _HASHTABLE_H_
#define _HASHTABLE_H_

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dog.h"

#define MAXTABLESIZE 1271

struct node_s{
char key [32];
int value;
struct node_s *next_node;
};
typedef struct node_s node_t;

struct hashtable_s{
int size;
struct node_s **table;
};
typedef struct hashtable_s hashtable_t;

hashtable_t *ht_create(int size){


int i;

if (size < 1)
return NULL;

hashtable_t *hashtable = malloc(sizeof(hashtable_t));


if(hashtable == NULL){
perror("Error inicializando hashtable" );
exit(-1);
}

hashtable->table = calloc(size, sizeof(node_t));


if(hashtable->table == NULL){
perror("Error inicializando hastable->table" );
exit(-1);
}

for(i = 0; i < size; i++)


hashtable->table[i] = NULL;

hashtable->size = size;

return hashtable;
}

int hash_function(char key [32] ){

int h = 0, i;
int len = strlen(key);
char temp[len+1];

for (i = 0; key[i]; i++)


temp[i] = toupper(key[i]);
temp[i]= '\0';

for(i = 0; i < 32; i++) {


if (temp[i]=='\0')

-1-
D:\Descargas\baangaritar-mtvelasquezg\baangaritar-mtvelasquezg\etc\hashtable.h jueves, 28 de abril de 2016 8:47 p. m.
break;
h = h + ( ( (int) temp[i] ) * (i+1) );
}

h = h % MAXTABLESIZE;
abs(h);
return h;
}

node_t *ht_newnode( char key[32], int value ) {

node_t *new_node;
new_node = malloc( sizeof( node_t ) );
if( new_node == NULL ) {
perror("Error al inicializar el nodo" );
exit(-1);
}

strcpy(new_node->key, key);
new_node->value = value;

if( new_node->key == NULL ) {


perror("Error en ht_newnode(), key = NULL" );
exit(-1);
}
new_node->next_node = NULL;

return new_node;
}

void put(hashtable_t *hashtable, char key[32], int value){

int index = hash_function(key);


node_t *new_node = ht_newnode(key, value);

if (!hashtable->table[index]) {
hashtable->table[index] = new_node;
} else {

node_t *next = hashtable->table[index];


while(next->next_node != NULL){
next = next->next_node;
}
next->next_node = new_node;

node_t *find(hashtable_t *hashtable,char key[32])


{
int bin = hash_function(key);
node_t *node = hashtable->table[bin];
return node;
}

void ht_free(hashtable_t *hashtable){


int i;
node_t *node, *next;
for (i = 0; i < MAXTABLESIZE; i++){
node = hashtable->table[i];
hashtable->table[i]=NULL;

-2-
D:\Descargas\baangaritar-mtvelasquezg\baangaritar-mtvelasquezg\etc\hashtable.h jueves, 28 de abril de 2016 8:47 p. m.
while (node != NULL) {
next = node;
node = node->next_node;
free(next);
}
}
}

void initialize_ht_file(hashtable_t *hashtable,char *path_file, int size_file){

int i;
FILE *fd;
fd = fopen (path_file, "r");
if(fd == NULL){
perror("Error en fopen()\n");
exit(-1);
}

for (i = 0; i < size_file; i++) {


dog_type *readed_dog;
readed_dog = malloc(sizeof(dog_type));
fseek(fd, sizeof(struct dogType) * i, SEEK_SET);
fread (readed_dog, sizeof(dog_type), 1, fd);
char key[32];
strcpy( key,readed_dog->name);
put(hashtable, key, i+1);
free(readed_dog);
}

#endif

-3-

S-ar putea să vă placă și