FiddleComputers

Hashing table essentials C

Jan 12th, 2020
134
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define TAILLE 20 //taile de la table de hachage
  6.  
  7. struct Element {
  8.    int donnees;  
  9.    int id;
  10. };
  11.  
  12. struct Element* tableHachage[TAILLE];
  13. struct Element* elVide;
  14. struct Element* el;
  15.  
  16. int hachage(int id) {
  17.    return id % TAILLE;
  18. }
  19.  
  20. struct Element *chercher(int id) {
  21.  
  22.    int posHachage = hachage(id);  
  23.    
  24.    while(tableHachage[posHachage] != NULL) {
  25.    
  26.       if(tableHachage[posHachage]->id == id)
  27.          return tableHachage[posHachage];
  28.            
  29.       ++posHachage;
  30.        
  31.       posHachage %= TAILLE;
  32.    }        
  33.    
  34.    return NULL;        
  35. }
  36.  
  37. void inserer(int id,int donnees) {
  38.  
  39.    struct Element *el = (struct Element*) malloc(sizeof(struct Element));
  40.    el->donnees = donnees;  
  41.    el->id = id;
  42.  
  43.    int posHachage = hachage(id);
  44.  
  45.    while(tableHachage[posHachage] != NULL && tableHachage[posHachage]->id != -1) {
  46.  
  47.       ++posHachage;
  48.        
  49.       posHachage %= TAILLE;
  50.    }
  51.    
  52.    tableHachage[posHachage] = el;
  53. }
  54.  
  55. struct Element* supprimer(struct Element* el) {
  56.    int id = el->id;
  57.  
  58.    int posHachage = hachage(id);
  59.  
  60.    while(tableHachage[posHachage] != NULL) {
  61.    
  62.       if(tableHachage[posHachage]->id == id) {
  63.          struct Element* temp = tableHachage[posHachage];
  64.            
  65.          tableHachage[posHachage] = elVide;
  66.          return temp;
  67.       }
  68.  
  69.       ++posHachage;
  70.        
  71.       posHachage %= TAILLE;
  72.    }      
  73.    
  74.    return NULL;        
  75. }
  76.  
  77. void afficher() {
  78.    int i = 0;
  79.    
  80.    for(i = 0; i<TAILLE; i++) {
  81.    
  82.       if(tableHachage[i] != NULL)
  83.          printf(" (%d,%d)",tableHachage[i]->id,tableHachage[i]->donnees);
  84.       else
  85.          printf(" ~~ ");
  86.    }
  87.    
  88.    printf("\n");
  89. }
  90.  
  91. int main() {
  92.    elVide = (struct Element*) malloc(sizeof(struct Element));
  93.    elVide->donnees = -1;  
  94.    elVide->id = -1;
  95.  
  96.    inserer(1, 20);
  97.    inserer(2, 70);
  98.    inserer(42, 80);
  99.    inserer(4, 25);
  100.    inserer(12, 44);
  101.    inserer(14, 32);
  102.    inserer(17, 11);
  103.    inserer(13, 78);
  104.    inserer(37, 97);
  105.  
  106.    afficher();
  107.    el = chercher(37);
  108.  
  109.    if(el != NULL) {
  110.       printf("Element trouve : %d\n", el->donnees);
  111.    } else {
  112.       printf("Element pas trouve\n");
  113.    }
  114.  
  115.    supprimer(el);
  116.    el = chercher(37);
  117.  
  118.    if(el != NULL) {
  119.       printf("Element trouve : %d\n", el->donnees);
  120.    } else {
  121.       printf("Element pas trouve \n");
  122.    }
  123. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×