0% ont trouvé ce document utile (0 vote)
4 vues2 pages

Gestion de table de hachage en C++

Transféré par

Shaba7 Maroc
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
4 vues2 pages

Gestion de table de hachage en C++

Transféré par

Shaba7 Maroc
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd

// *************realise par : $$ BOUSSOUIR AHMED $$*************

//************** [Link] *****************************************

#include<iostream>
#include<cmath>
using namespace std ;

struct Student{
string firstName;
string SecondName;
};

struct HashEntry{
int total;
string key;
Student *std;
};

// la fonction de hachage repartie en deux fonctions

int f(string key){


int i=0;
char c=key[i];
while(key[i]!='\0'){
c=c^key[++i];
}
return c;
}

int g(int c; int m){


return c% m ;
}

int hash(string key , int m ){


return g(f(key), m);
}

//****** la fonction qui initialise la table de hachage ******


void initialise(HashEntry *E,int m){
for(int i=0;i<m;i++){
E-->total=0;
E-->std=nullptr;
}
}

//****** la fonction qui ajoute une entrée clé-valeur à la table *******


int addEntry(HashEntry *E,int m ,student *std,string key){
int idx = hash(key , m);
int c=idx;
while(E[c].std!=nullptr){ //pour verifier si la case est deja utilise;
c++;
}
E[c].key=key;
E[c].std=std;
E[idx].total++;
return c;
}

//****** la fonction qui supprime une entrée de la table *******


int deleteEntry(HashEntry *E, int m , string key ){
int idx = hash(key ,m);
int c=idx;
while(key!=E[c].key){ //pour trouver l'indice ou l'etudiant est positione;
c++;
}
E[c].key='/0';
E[c].std=nullptr;
E[idx].total--;
return c;
}

//******* la fonction de recherche d’une clé ******


HashEntry *searchKey(string key , int m){
int idx = hash(key ,m);
int c=idx;
while(key!=E[c].key){ //pour trouver l'indice ou l'etudiant est positione;
c++;
}
return E+c;
}
// ****** test in main() *******
int main(){
int m=10;
int add;
HashEntry HashTable[m];*recherche;
student *talib;
string name;
initialise(HashTable,m);
add = addEntry(HashTable,m,talib,name);
recherche = searchKey(name,m);
del = deleteEntry(HashTable,m,name);
return 0;
}

Vous aimerez peut-être aussi