Advertisement
Arnab_Manna

HASHING Linerar Probing

Dec 8th, 2022 (edited)
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<String.h>
  4.  
  5. int m;
  6. int c;
  7.  
  8. int HashFn(int k,int i)
  9. {
  10.     return ((k+(i*c))%m);
  11. }
  12.  
  13. int isfull(int *a)
  14. {
  15.     int i,f=0;
  16.     for(i=0;i<m;i++)
  17.     {
  18.         if(a[i]!=-1)
  19.         {
  20.             f++;
  21.         }
  22.     }
  23.     if(f==0)
  24.     return 1;
  25.     else
  26.     return 0;
  27. }
  28.  
  29. int isempty(int *a)
  30. {
  31.     int i,f=0;
  32.     for(i=0;i<m;i++)
  33.     {
  34.         if(a[i]==-1)
  35.         {
  36.             f++;
  37.         }
  38.     }
  39.     if(f==12)
  40.     return 1;
  41.     else
  42.     return 0;
  43. }
  44. void printHash(int *a)
  45. {
  46.     int i;
  47.     puts("index\t\tdata\n");
  48.     for(i=0;i<m;i++)
  49.     {
  50.        
  51.         if(a[i]==-1)
  52.         printf("%d\t\t\n",i);
  53.         else
  54.         printf("%d\t\t%d\n",i,a[i]);
  55.     }
  56. }
  57.  
  58. void insert(int *a,int k)
  59. {
  60.     int i=0;
  61.     int h=HashFn(k,i);
  62.     while (a[h]!=-1)
  63.     {
  64.         if(isfull(a)==1)
  65.         {
  66.             printf("no space found ");
  67.             break;
  68.         }
  69.         i++;
  70.         h=HashFn(k,i);
  71.     }
  72.     a[h]=k;
  73.     puts("data added successfully");
  74.    
  75. }
  76.  
  77. void search(int *a,int n1)
  78. {
  79.     int i=0;
  80.     int h=HashFn(n1,i);
  81.     while (a[h]!=n1)
  82.     {
  83.         i++;
  84.         h=HashFn(n1,i);
  85.         if(a[h]==n1)
  86.         {
  87.             printf("the data is present at position%d\n",h);
  88.         }
  89.         else
  90.         {
  91.             printf("not found !\n");
  92.             break;
  93.         }
  94.     }
  95. }
  96.  
  97. void Delete(int *a,int n1)
  98. {
  99.     int i=0;
  100.     int h=HashFn(n1,i);
  101.     while (a[h]!=n1)
  102.     {
  103.         if(isempty(a)==1)
  104.         {
  105.             printf("no space found ");
  106.             break;
  107.         }
  108.         i++;
  109.         h=HashFn(n1,i);
  110.     }
  111.     if(a[h]==n1)
  112.     {
  113.         a[h]=-1;
  114.         printf("deleted\n");
  115.     }
  116.     else
  117.     {
  118.         printf("not found!\n");
  119.     }
  120. }
  121.  
  122. int main()
  123. {
  124.     int *a,i,n,ch;
  125.     puts("enter m :");
  126.     scanf("%d",&m);
  127.     a=(int*) calloc(m,sizeof(int));
  128.     puts("enter c :");
  129.     scanf("%d",&c);
  130.    
  131.     for(i=0;i<m;i++)
  132.     {
  133.         a[i]=-1;
  134.     }
  135.     while(1)
  136.     {
  137.         puts("1>insert a key\n2> search key\n3> delete a key\n4>display the hash table\n5>exit\nenter your choice :");
  138.         scanf("%d",&ch);
  139.         switch(ch)
  140.         {
  141.             case 1:
  142.                 printf("enter the data :");
  143.                 scanf("%d",&n);
  144.                 insert(a,n);
  145.             break;
  146.             case 2:
  147.                 printf("the data you want ? :");
  148.                 scanf("%d",&n);
  149.                 search(a,n);
  150.             break;
  151.             case 3:
  152.                 printf("the data you want to delete ? :");
  153.                 scanf("%d",&n);
  154.                 Delete(a,n);
  155.             break;
  156.             case 4:
  157.                 printHash(a);
  158.             break;
  159.             case 5:
  160.                 exit(0);
  161.             default:
  162.                 puts("invalid choice");
  163.         }
  164.     }
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement