Friday, July 20, 2012

Hashing To Strore Intergers From File And Search In C

 This is hashing program. That stores 100000 integers from file called hashInt.txt to an array using hashing.
you can download hashInt.txt file from the following link
download
If you are in trouble in understanding this please comment me
-------------------------------------------------------------------------------------
         Do you want to publish source codes in your blog or web site as follows.
                              Visit Source Code Formatter

 #include<stdio.h>  
 #include <stdlib.h>  
 #include <time.h>  
   
 #define ARR_SIZE 100000 //Number of integers in the file  
 #define TAB_SIZE 10000 //hash table size  
   
 int hash_func( int key);  
 int hash_search(int key,int value);  
   
 typedef struct hash_node{  
     
   int key;  
   struct hash_node *left;  
   struct hash_node *right;  
       
 }NODE;  
   
 int target_sum[]={231552,234756,596873,648219,726312,981237,988331,1277361,1283379};  
 NODE *hash_tab[TAB_SIZE];  
 int numbers[ARR_SIZE];  
   
 main(){  
   clock_t start = clock();  
   int i,k,key;  
   FILE *fr;  
   fr=fopen("HashInt.txt","r");  
   //intialize hash_tab array null  
   for (i=0;i<TAB_SIZE;i++){  
     hash_tab[i]=NULL;  
   }  
   i=0;  
   //get line by linee int  
   while(fscanf(fr,"%d",&key)!=EOF){  
     numbers[i]=key;  
     i++;  
     NODE *node=malloc(sizeof(NODE));  
     //get index of key  
     int index=hash_func(key);  
     //not collissions found  
     if(hash_tab[index]==NULL){  
       hash_tab[index]=node;  
       node->key=key;  
       node->right=NULL;  
       node->left=NULL;  
     }  
     //collission found  
     else{  
       NODE *p=hash_tab[index];  
       //binary search for an free node  
       while(p!=NULL){  
         if(p->key>key){  
           p=p->right;  
         }  
         else{  
           p=p->left;  
         }  
       }  
       p=node;  
       node->key=key;  
       node->right=NULL;  
       node->left=NULL;  
     }  
   }  
   fclose(fr);  
   //find target sums  
   for(k=0;k<9;k++){  
     int found=0;  
     for(i=0;i<ARR_SIZE;i++){  
       key=target_sum[k]-numbers[i];  
       if(key > 0){  
         if(hash_search(key,numbers[i])){  
           found=1;    
           printf("1");  
           break;  
         }  
       }  
     }  
     if(!found){  
       printf("0");  
     }  
   }  
   printf("\n");  
   printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);      
 }  
   
 //hash function  
 int hash_func( int key){  
   return key/100;  
 }  
 //hash search function  
 int hash_search(int key,int value){  
   int index=hash_func(key);  
   //index exeeds the array size  
   if(index>=TAB_SIZE){  
     return 0;  
   }  
     
   NODE *p=hash_tab[index];  
   //find key  
   if(p!=NULL){  
     int twise=0;  
     while(p!=NULL){  
       if(key==value){  
         if(p->key>key){  
           p=p->right;  
         }  
         else if(p->key<key){  
           p=p->left;  
         }  
         else{  
           if(twise){  
             return 0;  
           }  
           p=p->left;  
           twise++;  
         }  
       }  
       else{  
         if(p->key>key){  
           p=p->right;  
         }  
         else if(p->key<key){  
           p=p->left;  
         }  
         else{  
           return 1;  
         }  
       }  
     }  
     return 0;      
   }  
   else{  
     return 0;  
   }  
 }  

0 comments:

Post a Comment