Advertisement
Guest User

Untitled

a guest
May 31st, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.31 KB | None | 0 0
  1. #include "hash_table.h"
  2. #include <stdbool.h>
  3.  
  4. typedef struct element {  /*rappresenta un elemento della lista */
  5.   void* key; /* punta alla parola */
  6.   struct element* next;  /* prossimo elemento  */  
  7.   struct element* prev; /* elemento precedente */
  8. }element;
  9.  
  10. /* Restituisce un puntatore alla lista vuota */
  11. element* createList(){
  12.   return NULL;
  13. }
  14.  
  15. int charValue(char c){
  16.   return (c-'a')+1;
  17. }
  18.  
  19. /*Cerca nella lista se esiste il valore x*/
  20. element* findList(element *p, void* x){  
  21.   if(p == NULL){
  22.     return NULL;
  23.   }
  24.   if(p->key == x){
  25.     return p;
  26.   }
  27.   findList(p->next,x);
  28. }
  29. element* find_list_string(element *p, void* x){  
  30.   if(p == NULL){
  31.    
  32.     return NULL;
  33.   }
  34.   if(strcmp(p->key,x)==0){
  35.     printf("find_list_string=%s\n",x);
  36.     return p;
  37.   }  
  38.   findList(p->next,x);
  39.  
  40. }
  41.  
  42. /*Hash table- utilizzo la funzione di Horner che mi permette di calcolare
  43.   un polinomio in tempo O(n)*/
  44. int hash_string(char* w){  
  45.   int i;
  46.   int hashVal = 0;
  47.   for(i=0;w[i]!='\0';i++){
  48.     hashVal = 31 * hashVal + w[i];
  49.   }
  50.   hashVal = hashVal % HASHSIZE;
  51.   if(hashVal < 0)
  52.     hashVal += HASHSIZE;
  53.   return hashVal;
  54. }
  55. int hash(int x){
  56.   return x % HASHSIZE;
  57. }
  58. /* Elimina dalla lista p l'elemento il cui indirizzo e' p
  59.    e restituisce un puntatore alla  lista ottenuta */  
  60. element* deleteList(element *p, element * elemento_da_eliminare){
  61.     if(elemento_da_eliminare->prev != NULL) /* elemento_da_eliminare non e' il primo elemento */
  62.         elemento_da_eliminare->prev->next = elemento_da_eliminare->next;  
  63.     else  /* elemento_da_eliminare  e' il primo elemento */
  64.         p = elemento_da_eliminare->next;  /* la testa della lista cambia */
  65.     if(elemento_da_eliminare->next != NULL) /* elemento_da_eliminare non e' l'ultimo elemento */
  66.         elemento_da_eliminare->next->prev = elemento_da_eliminare->prev;
  67.     free(elemento_da_eliminare);
  68.     return p;
  69. }
  70.  
  71. element* insertList(element* p, void* x){
  72.   element* elemento_da_inserire =(element*) malloc(sizeof(element));  
  73.   elemento_da_inserire->key = x;
  74.   //strcpy(elemento_da_inserire->key,x);
  75.   elemento_da_inserire->next = p;
  76.   elemento_da_inserire->prev = NULL;
  77.   if(p != NULL)  /* p non e' vuota */
  78.         p->prev = elemento_da_inserire;
  79.   return elemento_da_inserire;
  80. }
  81. element* insert_list_string(element* p, void* x){
  82.   element* elemento_da_inserire =(element*) malloc(sizeof(element));  
  83.   elemento_da_inserire->key = x;
  84.   printf("insert_list_string=%s\n",elemento_da_inserire->key);
  85.   //strcpy(elemento_da_inserire->key,x);
  86.   elemento_da_inserire->next = p;
  87.   elemento_da_inserire->prev = NULL;
  88.   if(p != NULL)  /* p non e' vuota */
  89.         p->prev = elemento_da_inserire;
  90.   return elemento_da_inserire;
  91. }
  92.  
  93. /* Cancella dalla  tabella  table[] la parola x  */
  94. void  deleteTable(element** table, void* x){
  95.   long int index;
  96.   index = hash(x);  /* x se c'e' e' nella lista table[index]  */
  97.   element *q;  
  98.   if((q=findList(table[index],x)) != NULL){/* x  e' nella tabella */
  99.     table[index] = deleteList(table[index], q);
  100.   }else{/* x non è nella tabella */
  101.     printf("L'elemento da eliminare non è presente nell'HashTable\n");
  102.   }
  103. }
  104. void destroyList(element *p){
  105.     element *q=p;
  106.     while(q){  /*  while(q!= NULL) */
  107.         p = q->next;
  108.         //free(q->key);
  109.         free(q);
  110.         q = p;
  111.     }
  112. }
  113.  
  114. void destroyTable(element** table){
  115.   long int i;
  116.   for(i=0; i<HASHSIZE; i++){
  117.     destroyList(table[i]); /* cancella  la lista table[i] */
  118.     table[i]=createList();  /* table[i] e' la lista vuota */
  119.   }
  120. }
  121.  
  122. void insertTable(element** table, void* x){  
  123.   long int index;  
  124.   index = hash(x);  /*x va inserita nella lista table[index] */  
  125.   if(findList(table[index],x) == NULL){/* x non e' nella tabella */  
  126.     table[index] = insertList(table[index], x);
  127.   }else{
  128.    
  129.   }
  130. }
  131. void insert_table_string(element** table, void* x){  
  132.   printf("inser_table_string = %s\n",x);
  133.   int index;  
  134.   index = hash_string(x);  /*x va inserita nella lista table[index] */  
  135.   if(find_list_string(table[index],x) == NULL){/* x non e' nella tabella */  
  136.     table[index] = insert_list_string(table[index], x);
  137.   }else{
  138.    
  139.   }
  140. }
  141. void findTable(element** table,void* x){  
  142.   long int index;
  143.   index = hash(x);  
  144.   if(findList(table[index],x) != NULL){
  145.     printf("L'elemento [%d] è stato trovato nell'hash\n",x);
  146.   }else{
  147.     printf("L'elemento [%d] non è stato trovato nell'hash\n",x);
  148.   }
  149. }
  150. void printList(element* list){
  151.   if(list != NULL){
  152.     printf("%d\n",list->key);
  153.     printList(list->next);
  154.   }
  155. }
  156. void print_list_string(element* list){
  157.   if(list != NULL){
  158.     printf("%s\n",list->key);
  159.     print_list_string(list->next);
  160.   }
  161. }
  162. void printTable(element** table){
  163.   int i;
  164.   for(i=0;i<HASHSIZE;i++){
  165.     if(table[i] != NULL){
  166.       printf("table[%d] contiene:\n",i);    
  167.       printList(table[i]);
  168.     }else{
  169.       //printf("table non ha elementi\n");
  170.     }
  171.   }
  172. }
  173. void print_table_string(element** table){
  174.   int i;
  175.   for(i=0;i<HASHSIZE;i++){
  176.     if(table[i] != NULL){
  177.       printf("table[%d] contiene:\n",i);    
  178.       print_list_string(table[i]);
  179.     }else{
  180.       printf("table non ha elementi\n");
  181.     }
  182.   }
  183. }
  184.  
  185. /*Inizializza l'HashTable con elementi = NULL*/
  186. void initTable(element** table){
  187.  long int k;
  188.  for(k=0; k<HASHSIZE; k++)
  189.    table[k] = createList();
  190. }
  191. int compare_long_int(void* ptr1, void* ptr2) {
  192.   long int el1 = (long int) ptr1;
  193.   long int el2 = (long int) ptr2;
  194.   if(el1<el2) {
  195.     return -1;
  196.   }
  197.   if (el1 == el2) {
  198.     return 0;
  199.   }
  200.   return 1;
  201. }
  202.  
  203. void riempi_hash_int(element** table){  
  204.   int _interi,i,id;
  205.   double _double;
  206.   char string[60],stringa_supporto[60];  
  207.   FILE* fp;  
  208.   char* stringa_restituita;  
  209.   if((fp = fopen("/home/luca/Scrivania/Lab_Algoritmi/records.csv","r"))==NULL){
  210.     printf("Errore apertura file\n");    
  211.   }
  212.   while(i<NUM_MAX_RECORD) {    
  213.     stringa_restituita = fgets(stringa_supporto,60, fp);
  214.     if(stringa_restituita == NULL){
  215.       printf("Errore nella fgets\n");
  216.       break;
  217.     }        
  218.     sscanf(stringa_supporto,"%d,%[^,],%d,%lf",&id ,string,&(_interi),&(_double));    
  219.     insertTable(table,_interi);  
  220.     if(i%100000 == 0){
  221.       printf("I=%d\n",i);
  222.     }
  223.     i++;    
  224.   }  
  225.   fclose(fp);
  226. }
  227.  
  228. void riempi_hash_string(element** table){  
  229.   //puts("1");
  230.   int _interi,i,id;
  231.   double _double;
  232.   char string[60],stringa_supporto[60];  
  233.   FILE* fp;  
  234.   char* stringa_restituita;  
  235.   if((fp = fopen("/home/luca/Scrivania/Lab_Algoritmi/records.csv","r"))==NULL){
  236.     printf("Errore apertura file\n");    
  237.   }
  238.   while(i<NUM_MAX_RECORD) {  
  239.     //puts("2");  
  240.     stringa_restituita = fgets(stringa_supporto,60, fp);
  241.     if(stringa_restituita == NULL){
  242.       printf("Errore nella fgets\n");
  243.       break;
  244.     }  
  245.     //puts("3");      
  246.     sscanf(stringa_supporto,"%d,%[^,],%d,%lf",&id ,string,&(_interi),&(_double));
  247.     //puts("4");
  248.     printf("riempi_hash_string=%s\n",string);
  249.     //sleep("1");
  250.     insert_table_string(table,string);  
  251.     if(i%100000 == 0){
  252.       printf("I=%d\n",i);
  253.     }
  254.     i++;    
  255.   }  
  256.   fclose(fp);
  257. }
  258. int main(){
  259.   element** hashTable =(element**) malloc (sizeof(element*)*HASHSIZE);
  260.   initTable(hashTable);  
  261.   riempi_hash_string(hashTable);  
  262.   print_table_string(hashTable);
  263.   /*destroyTable(hashTable);
  264.   printTable(hashTable);*/
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement