Advertisement
Arnab_Manna

hashing Chaining

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