kebria

Hash Table Implementation

Jun 15th, 2021
613
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Hash Table Implementation Using C */
  2. #include<stdio.h>
  3.  
  4. #define size 7
  5. int arr[size];
  6.  
  7. void insert(int value)
  8. {
  9.     int key = value % size;
  10.  
  11.     if (arr[key] == -1)
  12.     {
  13.         arr[key] = value;
  14.         printf("%d inserted at arr[%d]\n", value, key);
  15.     }
  16.     else
  17.     {
  18.         printf("Collision : arr[%d] has element %d already!\n", key, arr[key]);
  19.         printf("Unable to insert %d\n", value);
  20.     }
  21. }
  22.  
  23. void del(int value)
  24. {
  25.     int key = value % size;
  26.     if (arr[key] == value)
  27.         arr[key] = -1;
  28.     else
  29.         printf("%d not present in the hash table\n", value);
  30. }
  31.  
  32. void search(int value)
  33. {
  34.     int key = value % size;
  35.     if (arr[key] == value)
  36.         printf("Search Found\n");
  37.     else
  38.         printf("Search Not Found\n");
  39. }
  40.  
  41. void print()
  42. {
  43.     int i;
  44.     for (i = 0; i < size; i++)
  45.         printf("arr[%d] = %d\n", i, arr[i]);
  46. }
  47.  
  48. int main()
  49. {
  50.     int i;
  51.     for (i = 0; i < size; i++)
  52.         arr[i] = -1;
  53.  
  54.     insert(10); //key = 10 % 7  ==> 3
  55.     insert(4);  //key =  4 % 7  ==> 4
  56.     insert(2);  //key =  2 % 7  ==> 2
  57.     insert(3);  //key =  3 % 7  ==> 3 (collision)
  58.  
  59.     printf("Hash table\n");
  60.     print();
  61.     printf("\n");
  62.  
  63.     printf("Deleting value 10\n");
  64.     del(10);
  65.     printf("After the deletion hash table\n");
  66.     print();
  67.     printf("\n");
  68.  
  69.     printf("Deleting value 5\n");
  70.     del(5);
  71.     printf("After the deletion hash table\n");
  72.     print();
  73.     printf("\n");
  74.  
  75.     printf("Searching value 4\n");
  76.     search(4);
  77.     printf("Searching value 10\n");
  78.     search(10);
  79.  
  80.     return 0;
  81. }
  82.  
  83. /*
  84. Output:
  85. 10 inserted at arr[3]
  86. 4 inserted at arr[4]
  87. 2 inserted at arr[2]
  88. Collision : arr[3] has element 10 already!
  89. Unable to insert 3
  90. Hash table
  91. arr[0] = -1
  92. arr[1] = -1
  93. arr[2] = 2
  94. arr[3] = 10
  95. arr[4] = 4
  96. arr[5] = -1
  97. arr[6] = -1
  98.  
  99. Deleting value 10
  100. After the deletion hash table
  101. arr[0] = -1
  102. arr[1] = -1
  103. arr[2] = 2
  104. arr[3] = -1
  105. arr[4] = 4
  106. arr[5] = -1
  107. arr[6] = -1
  108.  
  109. Deleting value 5
  110. 5 not present in the hash table
  111. After the deletion hash table
  112. arr[0] = -1
  113. arr[1] = -1
  114. arr[2] = 2
  115. arr[3] = -1
  116. arr[4] = 4
  117. arr[5] = -1
  118. arr[6] = -1
  119.  
  120. Searching value 4
  121. Search Found
  122. Searching value 10
  123. Search Not Found
  124. */
RAW Paste Data