Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it L'avatar di tagibo
    Registrato dal
    Sep 2005
    Messaggi
    62

    [C] Problema implementazione dell' inserimento in HashTable

    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

  2. #2
    Utente di HTML.it L'avatar di tagibo
    Registrato dal
    Sep 2005
    Messaggi
    62
    up

  3. #3
    Utente di HTML.it L'avatar di tagibo
    Registrato dal
    Sep 2005
    Messaggi
    62
    Help, qualcuno mi aiuti!!!

    Qualche esperto di C vede se c'è un errore??
    La compilazione non dà nessun erore, ma quando vado ad inserire un elemento si blocca tutto: ci deve essere quindi un errore logico da qualche parte! Ma dov'è?

  4. #4
    Utente di HTML.it L'avatar di ibykos
    Registrato dal
    Feb 2005
    Messaggi
    201
    Per favore posta tutto il codice, mi servono il main e la hashfunction per capire se il problema è altrove.

  5. #5
    Utente di HTML.it L'avatar di tagibo
    Registrato dal
    Sep 2005
    Messaggi
    62
    Grazie per la risposta, ti posto tutto.
    Fammi sapere!
    codice:
    int hashFunction (const char* s, hashTable_t* ht){
      int i=0,somma=0;
      while(s[i]!='\0'){
     	somma=somma+s[i];
    	i++;
      }
      somma=somma%(ht->size);
      if (i>MAXL) {fprintf(stderr,"Errore nel calcolo della hash function"); return -1;}
      return somma;
    }
    
    int main (int argc,char * argv []){
      hashTable_t * tabella=createHash (7);
      hashElement_t  * p1;
      const char * test="pippare";
      if ((p1=putHash(tabella,"rico",5))==NULL)printf ("inserimento non effettuato");
      if ((p1=putHash(tabella,"volare",7))==NULL)printf ("inserimento non effettuato");
      if ((p1=putHash(tabella,"pappare",8))==NULL)printf ("inserimento non effettuato");
      if ((p1=putHash(tabella,"pippare",2))==NULL)printf ("inserimento non effettuato");
      if ((p1=putHash(tabella,"parlate" ,51234))==NULL)printf ("inserimento non effettuato");
      return 0;
    }

  6. #6
    Utente di HTML.it L'avatar di ibykos
    Registrato dal
    Feb 2005
    Messaggi
    201
    M imancano ancora la getHash, gli include ed i define.

  7. #7
    Utente di HTML.it L'avatar di tagibo
    Registrato dal
    Sep 2005
    Messaggi
    62
    Sorry....
    codice:
    #include "lcshash.h" //nome file prototipi
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXL 256
    
    hashElement_t* getHash(hashTable_t* ht, const char* s){
    	int trovato=0;
    	hashElement_t* e=ht->table[hashFunction(s,ht)];
    
    	while(trovato==0 && e!=NULL){
    		if (strcmp(s,e->key) == 0) trovato=1;
    		else e=e->next;
    	}
    
    	if (trovato==0)return NULL;
    	else return e;
    }

  8. #8
    Utente di HTML.it L'avatar di ibykos
    Registrato dal
    Feb 2005
    Messaggi
    201
    Osserva questa sequenza:

    Tu dichiari una tabella inizialmente vuota, in cui tutti i puntatori puntano a NULL

    quando vuoi inserire un elemento chiami la funzione puthash la quale chiama la gethash la quale resituirà certamente NULL, visto che la tabella è vuota.

    La puthash uscirà sempre dal momento che la condizione di questo if
    codice:
    if (e==NULL)
        return NULL;
    è sempre vera per una tabella vuota.

  9. #9

    domanda!?!?!

    CIAO io sto facendo un programma per l'università, e data la scarsa fornitura di spiegazioni sulla creazione di hash table, ho trovato su internet il tuo programma. Ti chiedevo se potevo riutilizzare pezzi del tuo programma??
    Comunque il programma che sto facendo io è l'implementazione di un t9 per scrivere i messaggi.

    Inoltre avrei anche una domanda, qualcuno sa per caso se c'è un modo per bloccare una stringa, che dovrebbe essere il nome del file???
    faccio l'esempio:
    devo prendere i file ITALIANO.A, ITALIANO.B, ITALIANO.C
    come faccio a bloccare "italiano." e a variare solamente la A,B,C..... e così via con un cliclo for??

    GRAZIE IN ANTICIPO

  10. #10

    Scusa un altra domanda!!

    Scusate di nuovo....
    cosa viene fatto con questo???

    somma=somma%(ht->size); il duccio è come viene utilizzato quel %???

    si trova nella hashFunction!!!

    grazie ancora

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.