Sto cercando di implementare un HashTable, ma ho un problema con l'inserimento di un elemento nella tabella.
Qui di seguito vi posto il codice:
codice:
/*PROTOTIPI*/
typedef int typevalue_t;
/* tipo elemento della hash table */
typedef struct hashE{
char * key; /* chiave NULL se elemento vuoto*/
typevalue_t value; /* valore associato a key */
struct hashE * next;
} hashElement_t;
/* tipo della tabella hash (un array di puntatori a hashElement_t) */
typedef struct {
int size; /* ampiezza tabella */
hashElement_t ** table;
} hashTable_t;
codice:
/*Creazione di un hashtable*/
hashTable_t * createHash(int sizeH){
hashTable_t *ht;
if (sizeH<0||sizeH==0) return NULL;
ht = malloc(sizeof(hashTable_t));
if (ht==NULL) {
fprintf(stderr,"Errore");
return NULL;
}
ht->size=sizeH;
ht->table=malloc(sizeH*sizeof(hashElement_t*));
if (ht->table==NULL){
fprintf(stderr,"Errore");
return NULL;
}
return ht;
}
/*Inserimento di un elemento nell'hashtable*/
hashElement_t* putHash(hashTable_t* ht, const char * s, typevalue_t value){
int hashval=0;
hashElement_t *e;
hashval=hashFunction(s,ht);
e = getHash(ht,s);
if (e==NULL) return NULL;
else {
e = malloc(sizeof(hashElement_t));
if (e==NULL) return NULL;
e->key=strdup(s);
if (e->key == NULL) return NULL;
strcpy(e->key,s);
e->value = value;
e->next = ht->table[hashval];
ht->table[hashval] = e;
}
return e;
Qualcuno mi può aiutare a capire cosa non va?? :master:
thanks