You are on page 1of 2

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<math.

h> #define MAX 10 struct dct { int k; int val; }a[MAX]; void main() { int key,value,hkey,skey,choice; char ans; void init(); void insert(int,int,int); void display(); void size(); void search(int); int hashdivmethod(int); int hashmulmethod(int); clrscr(); init(); printf("\n dictionary functions using hashing\n"); do { printf("1:insertion(divmethod)\n 2:insertion(mulmethdo)\n 3:display\n 4:search by divmethod\n"); printf("enter ur choice\n"); scanf("%d",&choice); switch(choice) { case 1: printf("enter the key\n"); scanf("%d",&key); printf("enter the value\n"); scanf("%d",&value); hkey=hashdivmethod(key); insert(hkey,key,value); break; case 2: printf("enter the key\n"); scanf("%d",&key); printf("enter the value\n"); scanf("%d",&value); hkey=hashmulmethod(key); insert(hkey,key,value); break; case 3: display(); size(); break; case 4: printf("enter the key for search the record\n"); scanf("%d",&skey); search(skey); break;

} printf("do u want to continue\n"); ans=getch(); }while(ans=='y'); } void init() { int i; for(i=0;i<MAX;i++) { a[i].k=-1; a[i].val=-1; } } int hashdivmethod(int num) { int hkey; hkey=num%10; return hkey; } int hashmulmethod(int num) { int hkey; double a=0.6180; int p=1; hkey=floor(num*p*a); return hkey; } void insert(int index,int key,int value) { int flag,i,count=0; flag=0; if(a[index].k==-1) { a[index].k=key; a[index].val=value; } else { i=0; while(i<MAX) { if(a[i].k!=-1) count++; i++; } if(count==MAX) { printf("hash table is full\n"); } for(i=index+1;i<MAX;i++) if(a[i].k==-1) { a[i].k=key;

a[i].val=value; flag=1; break; } for(i=0;i<index&&flag==0;i++) if(a[i].k==-1) { a[i].k=key; a[i].val=value; flag=1; break; } } } void display() { int i; printf("the hash table is\n"); printf("\n**************************"); for(i=0;i<MAX;i++) { printf("\n"); printf("%d\t",i); printf("%d\t",a[i].k); printf("%d\t",a[i].val); } printf("\n**********************"); } void size() { int len=0,i; for(i=0;i<MAX;i++) { if(a[i].k!=-1) len++; } printf("the size of dictionary is\n"); printf("%d",len); } void search(int skey) { int i,j; i=hashdivmethod(skey); if(a[i].k==skey) { printf("the record is present at location\n"); return; } if(a[i].k!=skey) { for(j=i;j<MAX;j++) { if(a[j].k==skey) { printf("the record is present at location\n");

return; } } for(j=0;j<i;j++) { if(a[j].k==skey) { printf("the record is present at location\n"); return; } } } else printf("the record is not present\n"); }