// HASH VERSION 1.0 // tabla de tamanio 10 #include <stdio.h> #include <stdlib.h> #include <string.h> #define TAM 15 #define true 1 struct datos { char *Nombre; int Edad; }; struct datos Tabla[TAM]; void LimpiarTabla() { int x = 0; while( x < TAM ) { Tabla[x].Nombre = (char*)malloc(sizeof(char)*80); Tabla[x].Edad = 0; x++; } } void PideNombre(char *Var) { printf("Introduce el Nombre: "); scanf("%s", Var); } void PideEdad(int *Var) { printf("Introduce la edad: "); scanf("%d", Var); } int Hash(char *Nom) { int x=0, Suma = 0; while(x < strlen(Nom) ) { Suma += Nom[x]*x + strlen(Nom); Nom++; x++; } return Suma%TAM; } void Guarda(char *Nom, int Edad) { int Index=0; Index = Hash(Nom); while( Tabla[Index].Edad ) // si la edad=0, estA disponible el espacio { Index++; if (Index > 14) Index=0; } strcpy(Tabla[Index].Nombre, Nom); Tabla[Index].Edad = Edad; } void Guarda(char *Nom, int Edad) { } void MuestraTabla() { int x =0; char *CAD=(char*)malloc(sizeof(char)*100); system("clear"); printf("IDX\tNOMBRE\t\tEDAD\n"); while( x<TAM ) { strcpy(CAD, ""); sprintf(CAD, "%i.-\t\t%s\t%i", x, Tabla[x].Nombre, Tabla[x].Edad); printf("%s\n", CAD); x++; } } int Comando() { int Com = 0; printf("Que operaciOn deseas? [0|1] "); scanf("%d", &Com); return Com; } void Ciclo() { char *Nombre = (char*)malloc(sizeof(char)*80); int Idx=0, Edad = 0; while(true) { if ( Comando() ) // 1=guardar 0=consultar { PideNombre(Nombre); PideEdad(&Edad); Guarda(Nombre, Edad); MuestraTabla(); } else { // PREVER EN CASO DE COLISION PideNombre(Nombre); Idx = Hash(Nombre); printf("La edad de %s es de %i\n", Nombre, Tabla[Idx].Edad); } } } int main(int N, char **A) { LimpiarTabla(); Ciclo(); }