Sunday, 16 October 2011

CLOSED HASHING


PROGRAM:

//HASHING
#include<stdio.h>

int a[10],j=1;
         
void hash(int n,int ele)             //LINEAR PROBING
{
          int k;
          j=ele%10;
          while(a[j]!=0)
          {
                   j=(j%10)+1;
          }
          if(a[j]==0)
          {
                   a[j]=ele;
          }
}
void hash_quadratic(int n,int ele)     //QUADRATIC PROBING
{
        int i;
        j=ele%10;
        while(a[j]!=0)
        {
                for(i=1;i<11;i++)
                {
                        j=(ele+(i*i))%10;
                             if(j==0)       //instead of  array subscript starting from 0 this loop is implemented
                             {
                                      j=10;
                             }
                        if(a[j]==0)
                        {
                                break;
                        }
                }
        }


        if(a[j]==0)
        {
                a[j]=ele;

        }


}

void hash_double(int n,int ele)                   //DOUBLE HASHING
{
        int k;
        j=ele%10;
        if(a[j]!=0)
        {
                j=10-(ele%10);
                k=j;
        }
        while(a[j]!=0)
        {
                j=j+k;
         }
        if(a[j]==0)
        {
                a[j]=ele;
         }
}

void put(int n)                         //definition for put function
{
          int i;
          printf("output...\n");
          for(i=1;i<=10;i++)
          {
                   printf("%d\n",a[i]);
          }
}
         




int main()
{
          int n,ele,i,opt;
          printf("enter the number of elements\n");
          scanf("%d",&n);
          printf("enter the probing type...\n");
          printf("1.LINEAR\n");            //implementing menu driven manner
          printf("2.QUADRATIC\n");
          printf("3.DOUBLE HASHING\n");
          scanf("%d",&opt);
          switch(opt)
          {
                   case 1: printf("enter the elements\n");
                           for(i=0;i<n;i++)      //GETTING ELEMENTS FROM USER
                             {
                                      scanf("%d",&ele);
                             hash(n,ele);
                             }
                             put(n);
                   break;
                   case 2: printf("enter the elements\n");
                        for(i=0;i<n;i++)                  //GETTING ELEMENTS FROM USER
                        {
                                scanf("%d",&ele);
                                hash_quadratic(n,ele);
                        }
                        put(n);
                   break;
                   case 3: printf("enter the elements\n");
                        for(i=0;i<n;i++)                  //GETTING ELEMENTS FROM USER
                        {
                                scanf("%d",&ele);
                                hash_double(n,ele);
                        }
                        put(n);
                   break;

          default: printf("enter the correct option\n");
                    break;
          }
}

OUTPUT:

Enter the number of elements
6
Enter the probing type…
1.LINEAR
2.QUADRATIC
3.DOUBLE HASHING
1
Enter the elements
456
786
345
997
119
139
Output…
0
0
0
0
345
456
786
997
119
139
Enter the number of elements
8
Enter the probing type…
1.LINEAR
2.QUADRATIC
3.DOUBLE HASHING
2
Enter the elements
435
665
768
912
111
679
989
147
Output…
111
912
0
0
435
665
147
768
679
989
Enter the number of elements
7
Enter the probing type…
1.LINEAR
2.QUADRATIC
3.DOUBLE HASHING
3
Enter the elements
332
445
655
786
989
789
116
Output…
789
332
0
116
445
786
0
0
989
655

No comments:

Post a Comment