HASH_2.c



// TABLA HASH CON LISTAS LIGADAS EN COLISIONES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char nombres[25][50] =
	{"Juan","José Luis","José","María Guadalupe","Francisco",
	"Guadalupe","María","Juana","Antonio","Jesús","Miguel Ángel",
	"Pedro","Alejandro","Manuel","Margarita","María del Carmen",
	"Juan Carlos","Roberto","Fernando","Daniel","Carlos","Jorge",
	"Ricardo","Miguel"};

struct Nodo
{
	char *Nombre;
	struct Nodo *sig;
};

struct Nodo *Tabla[24];


int sumaascii(char *cad)
{
	int suma = 0;
	while ( cad[0] ) { suma += cad[0]; cad++; }
	return suma;
}

int F_HASH(int Ascii) { return Ascii%24; }

void Almacena(int F_H, char *Nombre)
{
	struct Nodo *aux, *new = (struct Nodo*)malloc(sizeof(struct Nodo));
	new->Nombre = (char*)malloc(sizeof(char)*50);
	strcpy(new->Nombre, Nombre); new->sig = NULL;

	if ( !Tabla[F_H] ) Tabla[F_H] = new;
	else
	{
		aux = Tabla[F_H];
		while (aux->sig) aux = aux->sig;
		aux->sig = new;
	}
}

void inicio()
{
	int x=0, sm=0, F_H=0;
	while (x<24)
	{
		Tabla[x] = NULL;
		x++;
	}
	x=0;
	while(x<24)
	{
		sm = sumaascii(nombres[x]);
		F_H = F_HASH(sm);
		printf("%i.- %s==>%i==>%i\n", (x+1), nombres[x], sm, F_H);
		Almacena(F_H, nombres[x]);
		x++;
	}
}

void Print_Tabla()
{
	int x=0;
	struct Nodo *nodo = NULL;
	printf("======================================\n");
	printf("____________TABLA HASH________________\n");
	printf("======================================\n");
	while(x<24)
	{
		nodo = Tabla[x];
		printf("%i.- ", x);
		while(nodo)
		{
			printf("%s\t", nodo->Nombre);
			nodo = nodo->sig;
		}
		printf("\n");
		x++;
	}
}

int main() { inicio(); Print_Tabla(); }