Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "hash_table.h"
- #include <stdbool.h>
- typedef struct element { /*rappresenta un elemento della lista */
- void* key; /* punta alla parola */
- struct element* next; /* prossimo elemento */
- struct element* prev; /* elemento precedente */
- }element;
- /* Restituisce un puntatore alla lista vuota */
- element* createList(){
- return NULL;
- }
- int charValue(char c){
- return (c-'a')+1;
- }
- /*Cerca nella lista se esiste il valore x*/
- element* findList(element *p, void* x){
- if(p == NULL){
- return NULL;
- }
- if(p->key == x){
- return p;
- }
- findList(p->next,x);
- }
- element* find_list_string(element *p, void* x){
- if(p == NULL){
- return NULL;
- }
- if(strcmp(p->key,x)==0){
- printf("find_list_string=%s\n",x);
- return p;
- }
- findList(p->next,x);
- }
- /*Hash table- utilizzo la funzione di Horner che mi permette di calcolare
- un polinomio in tempo O(n)*/
- int hash_string(char* w){
- int i;
- int hashVal = 0;
- for(i=0;w[i]!='\0';i++){
- hashVal = 31 * hashVal + w[i];
- }
- hashVal = hashVal % HASHSIZE;
- if(hashVal < 0)
- hashVal += HASHSIZE;
- return hashVal;
- }
- int hash(int x){
- return x % HASHSIZE;
- }
- /* Elimina dalla lista p l'elemento il cui indirizzo e' p
- e restituisce un puntatore alla lista ottenuta */
- element* deleteList(element *p, element * elemento_da_eliminare){
- if(elemento_da_eliminare->prev != NULL) /* elemento_da_eliminare non e' il primo elemento */
- elemento_da_eliminare->prev->next = elemento_da_eliminare->next;
- else /* elemento_da_eliminare e' il primo elemento */
- p = elemento_da_eliminare->next; /* la testa della lista cambia */
- if(elemento_da_eliminare->next != NULL) /* elemento_da_eliminare non e' l'ultimo elemento */
- elemento_da_eliminare->next->prev = elemento_da_eliminare->prev;
- free(elemento_da_eliminare);
- return p;
- }
- element* insertList(element* p, void* x){
- element* elemento_da_inserire =(element*) malloc(sizeof(element));
- elemento_da_inserire->key = x;
- //strcpy(elemento_da_inserire->key,x);
- elemento_da_inserire->next = p;
- elemento_da_inserire->prev = NULL;
- if(p != NULL) /* p non e' vuota */
- p->prev = elemento_da_inserire;
- return elemento_da_inserire;
- }
- element* insert_list_string(element* p, void* x){
- element* elemento_da_inserire =(element*) malloc(sizeof(element));
- elemento_da_inserire->key = x;
- printf("insert_list_string=%s\n",elemento_da_inserire->key);
- //strcpy(elemento_da_inserire->key,x);
- elemento_da_inserire->next = p;
- elemento_da_inserire->prev = NULL;
- if(p != NULL) /* p non e' vuota */
- p->prev = elemento_da_inserire;
- return elemento_da_inserire;
- }
- /* Cancella dalla tabella table[] la parola x */
- void deleteTable(element** table, void* x){
- long int index;
- index = hash(x); /* x se c'e' e' nella lista table[index] */
- element *q;
- if((q=findList(table[index],x)) != NULL){/* x e' nella tabella */
- table[index] = deleteList(table[index], q);
- }else{/* x non è nella tabella */
- printf("L'elemento da eliminare non è presente nell'HashTable\n");
- }
- }
- void destroyList(element *p){
- element *q=p;
- while(q){ /* while(q!= NULL) */
- p = q->next;
- //free(q->key);
- free(q);
- q = p;
- }
- }
- void destroyTable(element** table){
- long int i;
- for(i=0; i<HASHSIZE; i++){
- destroyList(table[i]); /* cancella la lista table[i] */
- table[i]=createList(); /* table[i] e' la lista vuota */
- }
- }
- void insertTable(element** table, void* x){
- long int index;
- index = hash(x); /*x va inserita nella lista table[index] */
- if(findList(table[index],x) == NULL){/* x non e' nella tabella */
- table[index] = insertList(table[index], x);
- }else{
- }
- }
- void insert_table_string(element** table, void* x){
- printf("inser_table_string = %s\n",x);
- int index;
- index = hash_string(x); /*x va inserita nella lista table[index] */
- if(find_list_string(table[index],x) == NULL){/* x non e' nella tabella */
- table[index] = insert_list_string(table[index], x);
- }else{
- }
- }
- void findTable(element** table,void* x){
- long int index;
- index = hash(x);
- if(findList(table[index],x) != NULL){
- printf("L'elemento [%d] è stato trovato nell'hash\n",x);
- }else{
- printf("L'elemento [%d] non è stato trovato nell'hash\n",x);
- }
- }
- void printList(element* list){
- if(list != NULL){
- printf("%d\n",list->key);
- printList(list->next);
- }
- }
- void print_list_string(element* list){
- if(list != NULL){
- printf("%s\n",list->key);
- print_list_string(list->next);
- }
- }
- void printTable(element** table){
- int i;
- for(i=0;i<HASHSIZE;i++){
- if(table[i] != NULL){
- printf("table[%d] contiene:\n",i);
- printList(table[i]);
- }else{
- //printf("table non ha elementi\n");
- }
- }
- }
- void print_table_string(element** table){
- int i;
- for(i=0;i<HASHSIZE;i++){
- if(table[i] != NULL){
- printf("table[%d] contiene:\n",i);
- print_list_string(table[i]);
- }else{
- printf("table non ha elementi\n");
- }
- }
- }
- /*Inizializza l'HashTable con elementi = NULL*/
- void initTable(element** table){
- long int k;
- for(k=0; k<HASHSIZE; k++)
- table[k] = createList();
- }
- int compare_long_int(void* ptr1, void* ptr2) {
- long int el1 = (long int) ptr1;
- long int el2 = (long int) ptr2;
- if(el1<el2) {
- return -1;
- }
- if (el1 == el2) {
- return 0;
- }
- return 1;
- }
- void riempi_hash_int(element** table){
- int _interi,i,id;
- double _double;
- char string[60],stringa_supporto[60];
- FILE* fp;
- char* stringa_restituita;
- if((fp = fopen("/home/luca/Scrivania/Lab_Algoritmi/records.csv","r"))==NULL){
- printf("Errore apertura file\n");
- }
- while(i<NUM_MAX_RECORD) {
- stringa_restituita = fgets(stringa_supporto,60, fp);
- if(stringa_restituita == NULL){
- printf("Errore nella fgets\n");
- break;
- }
- sscanf(stringa_supporto,"%d,%[^,],%d,%lf",&id ,string,&(_interi),&(_double));
- insertTable(table,_interi);
- if(i%100000 == 0){
- printf("I=%d\n",i);
- }
- i++;
- }
- fclose(fp);
- }
- void riempi_hash_string(element** table){
- //puts("1");
- int _interi,i,id;
- double _double;
- char string[60],stringa_supporto[60];
- FILE* fp;
- char* stringa_restituita;
- if((fp = fopen("/home/luca/Scrivania/Lab_Algoritmi/records.csv","r"))==NULL){
- printf("Errore apertura file\n");
- }
- while(i<NUM_MAX_RECORD) {
- //puts("2");
- stringa_restituita = fgets(stringa_supporto,60, fp);
- if(stringa_restituita == NULL){
- printf("Errore nella fgets\n");
- break;
- }
- //puts("3");
- sscanf(stringa_supporto,"%d,%[^,],%d,%lf",&id ,string,&(_interi),&(_double));
- //puts("4");
- printf("riempi_hash_string=%s\n",string);
- //sleep("1");
- insert_table_string(table,string);
- if(i%100000 == 0){
- printf("I=%d\n",i);
- }
- i++;
- }
- fclose(fp);
- }
- int main(){
- element** hashTable =(element**) malloc (sizeof(element*)*HASHSIZE);
- initTable(hashTable);
- riempi_hash_string(hashTable);
- print_table_string(hashTable);
- /*destroyTable(hashTable);
- printTable(hashTable);*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement