1 /******************************************************************************/
2 /* Hachage linéaire avec suppression */
3 /******************************************************************************/
13 typedef struct {int cle
;
17 void affiche_tab(t_elem
*tab
)
19 for (i
= 0 ; i
<M
; i
++) printf("<%2d,%2d>",tab
[i
].cle
,tab
[i
].etat
);
23 int h(int x
) /* mauvaise fonction de hachage - juste pour tester */
26 int rechercher(int x
,t_elem
*tab
)
27 /* retourne l'indice de x dans la table si x est present, -1 sinon */
30 if ((tab
[v
].etat
==VIDE
) || ((tab
[v
].etat
==OCCUPE
) &&
33 else {i
++;v
++;if (v
>=M
) v
=0;
36 if ((tab
[v
].etat
==OCCUPE
) && (tab
[v
].cle
==x
)) /* x présent */ return v
;
37 else /* x absent */ return -1;
40 int supprimer(int x
,t_elem
*tab
)
41 /* supprime x de la table, s'il est présent (et alors retourne son indice),
42 et ne fait rien si x est absent (et alors retourne -1)
44 {int v
= rechercher(x
,tab
);
45 if (v
!=-1) /*x present */ {tab
[v
].etat
=LIBERE
; return v
;
47 else /* x absent */ return -1;
50 int inserer(int x
,t_elem
*tab
)
51 /* si x n'est pas dans la table, on l'insère a la première place vide ou liberee
52 rencontrée au cours des essais successifs (et on retourne son indice);
53 si x est déjà dans la table, on ne fait rien (et on retourne son indice);
54 si le tableau est plein, c'est que x n'a pu être insèré et on retourne -1 */
55 {int i
=0,lib
=-1,v
=h(x
);
57 {if (tab
[v
].etat
==VIDE
) break;
58 /*x absent, on arrête le parcours du tableau */
59 if ((tab
[v
].etat
==OCCUPE
) && (tab
[v
].cle
==x
)) return v
;
60 /* x déjà présent, on ne fait rien */
61 if ((tab
[v
].etat
==LIBERE
) && (lib
==-1)) lib
=v
;
62 /* on mémorise l'indice de la première place libérée */
63 i
++;v
++;if (v
>=M
) v
=0;
64 /* on continue tant qu'on ne trouve pas x ou une place vide */
67 if (tab
[v
].etat
==VIDE
)
68 if (lib
==-1) {tab
[v
].cle
=x
;tab
[v
].etat
=OCCUPE
;return v
;}
69 else {tab
[lib
].cle
=x
;tab
[lib
].etat
=OCCUPE
;return lib
;}
77 tab
= (t_elem
*)calloc((M
),sizeof(t_elem
));
78 if (tab
== NULL
) {printf("memoire insuffisante\n"); exit(-1); }
80 for (i
=0;i
<M
;i
++) tab
[i
].etat
= VIDE
;
83 printf("%d\n",inserer(3,tab
)); affiche_tab(tab
);
84 printf("%d\n",inserer(5,tab
)); affiche_tab(tab
);
85 printf("%d\n",inserer(13,tab
)); affiche_tab(tab
);
86 printf("%d\n",inserer(9,tab
)); affiche_tab(tab
);
87 printf("%d\n",inserer(19,tab
)); affiche_tab(tab
);
88 printf("%d\n",supprimer(3,tab
)); affiche_tab(tab
);
89 printf("%d\n",rechercher(3,tab
)); affiche_tab(tab
);
90 printf("%d\n",inserer(23,tab
)); affiche_tab(tab
);
91 printf("%d\n",inserer(3,tab
)); affiche_tab(tab
);
93 /*****************************************************************************
94 <0,0> <0,0> <0,0> <0,0> <0,0> <0,0> <0,0> <0,0> <0,0> <0,0>
96 <0,0> <0,0> <0,0> <3,1> <0,0> <0,0> <0,0> <0,0> <0,0> <0,0>
98 <0,0> <0,0> <0,0> <3,1> <0,0> <5,1> <0,0> <0,0> <0,0> <0,0>
100 <0,0> <0,0> <0,0> <3,1> <13,1> <5,1> <0,0> <0,0> <0,0> <0,0>
102 <0,0> <0,0> <0,0> <3,1> <13,1> <5,1> <0,0> <0,0> <0,0> <9,1>
104 <19,1> <0,0> <0,0> <3,1> <13,1> <5,1> <0,0> <0,0> <0,0> <9,1>
106 <19,1> <0,0> <0,0> <3,-1> <13,1> <5,1> <0,0> <0,0> <0,0> <9,1>
108 <19,1> <0,0> <0,0> <3,-1> <13,1> <5,1> <0,0> <0,0> <0,0> <9,1>
110 <19,1> <0,0> <0,0> <23,1> <13,1> <5,1> <0,0> <0,0> <0,0> <9,1>
112 <19,1> <0,0> <0,0> <23,1> <13,1> <5,1> <3,1> <0,0> <0,0> <9,1>
114 *****************************************************************************/