Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define TAILLE 20 //taile de la table de hachage
- struct Element {
- int donnees;
- int id;
- };
- struct Element* tableHachage[TAILLE];
- struct Element* elVide;
- struct Element* el;
- int hachage(int id) {
- return id % TAILLE;
- }
- struct Element *chercher(int id) {
- int posHachage = hachage(id);
- while(tableHachage[posHachage] != NULL) {
- if(tableHachage[posHachage]->id == id)
- return tableHachage[posHachage];
- ++posHachage;
- posHachage %= TAILLE;
- }
- return NULL;
- }
- void inserer(int id,int donnees) {
- struct Element *el = (struct Element*) malloc(sizeof(struct Element));
- el->donnees = donnees;
- el->id = id;
- int posHachage = hachage(id);
- while(tableHachage[posHachage] != NULL && tableHachage[posHachage]->id != -1) {
- ++posHachage;
- posHachage %= TAILLE;
- }
- tableHachage[posHachage] = el;
- }
- struct Element* supprimer(struct Element* el) {
- int id = el->id;
- int posHachage = hachage(id);
- while(tableHachage[posHachage] != NULL) {
- if(tableHachage[posHachage]->id == id) {
- struct Element* temp = tableHachage[posHachage];
- tableHachage[posHachage] = elVide;
- return temp;
- }
- ++posHachage;
- posHachage %= TAILLE;
- }
- return NULL;
- }
- void afficher() {
- int i = 0;
- for(i = 0; i<TAILLE; i++) {
- if(tableHachage[i] != NULL)
- printf(" (%d,%d)",tableHachage[i]->id,tableHachage[i]->donnees);
- else
- printf(" ~~ ");
- }
- printf("\n");
- }
- int main() {
- elVide = (struct Element*) malloc(sizeof(struct Element));
- elVide->donnees = -1;
- elVide->id = -1;
- inserer(1, 20);
- inserer(2, 70);
- inserer(42, 80);
- inserer(4, 25);
- inserer(12, 44);
- inserer(14, 32);
- inserer(17, 11);
- inserer(13, 78);
- inserer(37, 97);
- afficher();
- el = chercher(37);
- if(el != NULL) {
- printf("Element trouve : %d\n", el->donnees);
- } else {
- printf("Element pas trouve\n");
- }
- supprimer(el);
- el = chercher(37);
- if(el != NULL) {
- printf("Element trouve : %d\n", el->donnees);
- } else {
- printf("Element pas trouve \n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement