Advertisement
Shailrshah

Hashing

Nov 8th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #define SIZE 13
  3. int arr[SIZE] = {0};
  4. int search(int value, int type){
  5.     int index, i = 0, count = 0;
  6.     index = value % SIZE;
  7.     while(arr[index] != value && count <= SIZE){
  8.         count++;
  9.         if(index >= SIZE) index %= SIZE;
  10.         else{
  11.             if(type == 1)  index += (1);
  12.             if(type == 2)  index += (++i * i);
  13.             if(type == 3)  index += (7 - (index % 7));
  14.         }
  15.     }
  16.     if(count > SIZE){
  17.         printf("\nThe appropriate index is not found.");
  18.         return -1;
  19.     }
  20.     return index;
  21. }
  22. void display(){
  23.   for(int i = 0; i < SIZE; i++)
  24.     if(arr[i] != 0)  printf("arr[%d] = %d\n", i, arr[i]);
  25. }
  26. int main(){
  27.     int choice, n, type, result;
  28.     while(1){
  29.         display();
  30.         printf("\n\n1.Insert 2.Delete 3.Search 4.Exit: ");
  31.         scanf("%d", &choice);
  32.         switch(choice){
  33.             case 1: printf("\nEnter a value: ");
  34.                     scanf("%d", &n);
  35.                     printf("\n1.Linear Probing 2.Quadratic Probing 3.Double Hashing: ");
  36.                     scanf("%d", &type);
  37.                     arr[search(0, type)] = n;
  38.                     break;
  39.             case 2: printf("\nEnter a value to be deleted: ");
  40.                     scanf("%d", &n);
  41.                     arr[search(n, 1)] = 0;
  42.                     break;
  43.             case 3: printf("\nEnter a value to be searched: ");
  44.                     scanf("%d", &n);
  45.                     printf("\n1.Linear Probing 2.Quadratic Probing 3.Double Hashing: ");
  46.                     scanf("%d", &type);
  47.                     result = search(n,type);
  48.                     if(result+1)printf("Found at %d.", result);
  49.                     break;
  50.             case 4: return 0;
  51.             default:printf("\nInvalid Choice.");
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement