C code to implement hash using array.
Description :
Hash can store only integer value. One slot can store only one value. Hash function is mod ( value % slots) .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* int hash code using array by Codingstreet.com */ /* hash function (key % hashize) */ #include<stdio.h> int *create_hash(int hashsize){ int p; p=(int *)malloc(sizeof(int)* hashsize); return p; } int main (){ int *h1,h1size,i=0,key,val,slot; printf("\n Enter the hash size : "); scanf("%d",&h1size); h1=create_hash(h1size); printf("\n Please not that we are using array so value which you are going to enter may override the older value because of getting same hash value. "); do{ printf("\n Hash Menu by Codingstreet.com "); printf("\n 1. Insert value"); printf("\n 2. Find Value "); printf("\n 3. Exit"); printf("\n Enter the key : "); scanf("%d",&key); switch(key){ case 1: printf("\n Enter the value :"); scanf("%d",&val); slot=val%h1size;//computed hash key using mod function h1[slot]=val; printf("\n Slot for value %d is %d",val,slot); break; case 2: printf("\n Enter the value to be searched :"); scanf("%d",&val); slot=val%h1size; if(h1[slot]==val) printf("\n %d is found at slot %d ",val,slot); else printf("\n %d is not found ",val); break; case 3: printf(" \n Good Bye .. "); break; } }while(key!=3); printf("\n Thank you for using codingstreet.com 's DataStructure Solutions\n"); return 0;; } |