HASH_1.c



// EJEMPLO DE CODIGO HASH CON COLISIONES

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

#define TAM 10
#define TAM_VECTOR 10

struct Colision
{
	char *Nombre;
	char *Apellido;
	struct Colision *Sig;
};

struct Dato
{
	char *Nombre;
	char *Apellido;
	struct Colision *Lista;
};

struct Dato VECTOR[TAM_VECTOR];

char *SubString(char *CAD, char Ini, char Fin)
{
	int k = 0, Pos=0;
	char *SUB_STR =  (char *)malloc(sizeof(char)*50);

	while (CAD[k] != Ini) k++;  k++;
	while(CAD[k] != Fin) { SUB_STR[Pos] = CAD[k]; Pos++; k++;  }
	SUB_STR[Pos] = '\0'; 
	return SUB_STR;
}


char *Reserva( ) { return (char *)malloc(sizeof(char)*50); }

int Codigo_Hash(char *Nombre)
{
	int k=0, Suma = 0;			// Suma de los cOdigos ascii

	while(Nombre[k] != '\0') { Suma += (int)Nombre[k]; k++; }
	return Suma % TAM_VECTOR;
}

void Almacena(int Indice, char *Nombre, char *Apellido)
{
	struct Colision *AUX, *PREV;

	if(VECTOR[Indice].Nombre == NULL)
	{
		VECTOR[Indice].Nombre = Reserva();
		VECTOR[Indice].Apellido = Reserva();
		VECTOR[Indice].Lista = NULL;
		strcpy(VECTOR[Indice].Nombre, Nombre);
		strcpy(VECTOR[Indice].Apellido, Apellido);
	}
			// Y si sI hubo algo? (colisiOn)
	else
	{
		if (VECTOR[Indice].Lista != NULL) 
		{
			AUX = VECTOR[Indice].Lista;
			while(AUX != NULL) { PREV = AUX; AUX = AUX->Sig; }
			AUX = (struct Colision*)malloc(sizeof(struct Colision));
			AUX->Nombre = Reserva();
			AUX->Apellido = Reserva();
			strcpy(AUX->Nombre, Nombre);
			strcpy(AUX->Apellido, Apellido);
			AUX->Sig = NULL;
			PREV->Sig = AUX;
		}
		else
		{
			AUX = (struct Colision*)malloc(sizeof(struct Colision));
			AUX->Nombre = Reserva();
			AUX->Apellido = Reserva();
			strcpy(AUX->Nombre, Nombre);
			strcpy(AUX->Apellido, Apellido);
			AUX->Sig = NULL;
			VECTOR[Indice].Lista = AUX;
		}
	}
}

void Ejecuta_Almacena(char *CAD)
{
	char *Nombre = Reserva();
	char *Apellido =  Reserva();
	Nombre = SubString(CAD, '(', ',');
	Apellido = SubString(CAD, ',', ')');
	Almacena(Codigo_Hash(Nombre), Nombre, Apellido);
}

void Ejecuta_Muestra( )
{
	int k=0;
	struct Colision *AUX = NULL;

	while(k < TAM_VECTOR)
	{
		if (VECTOR[k].Nombre != NULL)
		{
			printf("%s ", VECTOR[k].Nombre);
			printf("%s ", VECTOR[k].Apellido);
			if(VECTOR[k].Lista != NULL)
			{
				AUX = VECTOR[k].Lista;
				while(AUX != NULL)
				{
					printf("{==} %s ", AUX->Nombre);
					printf(" %s ", AUX->Apellido);
					AUX = AUX->Sig;
				}
			} printf("\n");
		}
		k++;
	}
}

int Interpreta(char *CAD)
{
	char Comando[TAM], Nombre[TAM], Apellido[TAM];
	int k = 0;

	while (CAD[k] != '(') { Comando[k] = CAD[k]; k++; } Comando[k] = '\0';
	if (!strncmp(Comando, "Almacena", 8)) Ejecuta_Almacena(CAD);
	if (!strncmp(Comando, "Busca", 5))  printf("Busca\n"); //Ejecuta_Busca(CAD);
	if (!strncmp(Comando, "Muestra", 7))  Ejecuta_Muestra( );
	if (!strncmp(Comando, "Fin", 3)) return 0;
	return 1;
}

void Ciclo( )
{
	int Seguir = 1;
	char *CAD = Reserva();

	while(Seguir)
	{
		printf("==> "); gets(CAD);
		Seguir = Interpreta(CAD);
	}
}

int main() { Ciclo(); printf("Ok \n"); }