Created
January 13, 2017 18:09
-
-
Save ibabde/1c13938a0f267e8a79710123df79d57f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#define N 100 | |
typedef struct Eleve { | |
char* nom; | |
int age; | |
struct Eleve* next; | |
} Eleve; | |
Eleve* hash_table[N]= {NULL}; | |
int hash_function(char *s) { | |
int hash = 0; | |
for(;*s != '\0';s++) | |
hash += *s; | |
return hash%N; | |
} | |
Eleve* createEleve(char* nom,int age) { | |
Eleve* e = malloc(sizeof(Eleve)); | |
e->nom = nom; | |
e->age = age; | |
e->next = NULL; | |
return e; | |
} | |
void addHashTable(char* nom,int age) { | |
Eleve* e = createEleve(nom,age); | |
Eleve** in_pos = &hash_table[hash_function(nom)]; | |
while(*in_pos != NULL) | |
in_pos = &(*in_pos)->next; | |
*in_pos = e; | |
} | |
void addHashTableAddressage(char* nom,int age) { | |
Eleve* e = createEleve(nom,age); | |
int pos = hash_function(nom); | |
int i = pos; | |
do { | |
if(hash_table[i] == NULL) { | |
hash_table[i] = e; | |
return; | |
} | |
i = (i+1)%N; | |
} while(i != pos); | |
//erreur | |
} | |
int search(Eleve* e) { | |
Eleve* tmp = hash_table[hash_function(e->nom)]; | |
while(tmp != NULL) { | |
if(strcmp(tmp->nom,e->nom) == 0 && tmp->age == e->age) | |
return 1; | |
tmp = tmp->next; | |
} | |
return 0; | |
} | |
void afficherHashTable() { | |
int i; | |
Eleve* tmp; | |
for(i=0;i < N;i++) | |
if(hash_table[i] != NULL) { | |
tmp = hash_table[i]; | |
printf("position %d : ",i); | |
while(tmp != NULL) { | |
printf("%s %d\t",tmp->nom,tmp->age); | |
tmp = tmp->next; | |
} | |
printf("\n"); | |
} | |
} | |
int main() { | |
addHashTableAddressage("c",18); | |
addHashTableAddressage("c",20); | |
addHashTable("c",25); | |
afficherHashTable(); | |
printf("recherche : %d\n",search(createEleve("ibrahim",20))); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment