1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef enum {LIBRE, DISPO, OCCUPE} Etat;
typedef struct tab
{
char **Tab;
int Taille;
Etat *Flag;
} Hachage;
Hachage *Alloue (int n)
{
int i;
/* On teste la valeur du parametre qui representera la taille de notre tableau */
if (n<0 || n==0) {fprintf(stderr,"Erreur de la taille pour le tableau\n");return NULL;}
/* On alloue la structure */
Hachage *T = malloc(sizeof (Hachage));
if (!T) {fprintf(stderr,"Erreur lors de l'allocation de la strucutre Hachage\n");return NULL;}
/* On alloue le tableau */
T->Taille = n;
T->Tab = NULL; /* Initialiser !! */
/* On initialise les cases du flag */
T->Flag = malloc(sizeof(int)*T->Taille);
for (i=0;i<T->Taille;i++)
T->Flag[i] = LIBRE;
/* On retourne le pointeur sur la structure initialisé */
return T;
int Hache (Hachage *T, char *cle)
{
int ValeurHache=0;
int i;
while (cle[i] != '\0')
{
ValeurHache += cle[i] ;
i++;
}
return ValeurHache;
}
int Ajoute (Hachage *T, char *mot)
{
int hache = Hache (T,mot);
int cpt=0;
while (T->Flag[i] == OCCUPE)
{
hache = (hache+1)%T->Taille;
cpt++;
if (cpt == hache)
return -1;
}
T->Tab[hache] = strdup(mot);
T->Flag[hache] = OCCUPE;
return 0;
} |
Partager