aniksajli

HashingWithChaining

Sep 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5.  
  6. #define SIZE 20
  7.  
  8. struct DataItem {
  9. int data;
  10. int key;
  11. };
  12.  
  13. struct DataItem* dataList[SIZE][SIZE];
  14. struct DataItem* singleItem;
  15. struct DataItem* item;
  16.  
  17. int getHashCode(int key) {
  18. return key%SIZE;
  19. }
  20.  
  21. struct DataItem *search(int key) {
  22. //get the hash
  23. int hashIndex = getHashCode(key);
  24. int d = 0;
  25. //move in array until an empty
  26. while(dataList[hashIndex][d]!=NULL) {
  27. if(dataList[hashIndex][d]->key==key)
  28. return dataList[hashIndex][d];
  29. //go to next cell
  30. ++d;
  31. //wrap around the table
  32. hashIndex %= SIZE;
  33. }
  34. return NULL;
  35. };
  36.  
  37. void insert(int key,int data) {
  38. struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
  39. item->data=data;
  40. item->key = key;
  41.  
  42. //get the hash
  43. int hashIndex = getHashCode(key);
  44. int d = 0;
  45.  
  46. //move in array until an empty or deleted cell
  47. while(dataList[hashIndex][d] != NULL && dataList[hashIndex][d]->key != -1) {
  48. //goto next call
  49. ++d;
  50.  
  51. //wrap around the table
  52. hashIndex %= SIZE;
  53. }
  54. dataList[hashIndex][d] = item;
  55. }
  56.  
  57. struct DataItem* del(struct DataItem* item) {
  58. int key = item->key;
  59.  
  60. //get the hash
  61. int hashIndex = getHashCode(key);
  62. int d = 0;
  63. //move in array until an empty
  64. while(dataList[hashIndex][d] != NULL) {
  65. if(dataList[hashIndex][d]->key ==key) {
  66. struct DataItem* temp = dataList[hashIndex][d];
  67.  
  68. //assign a dummy item at deleted position
  69. dataList[hashIndex][d] = singleItem;
  70. return temp;
  71. }
  72. //go to next cell
  73. ++d;
  74. //wrap around the table
  75. hashIndex %= SIZE;
  76. }
  77. return NULL;
  78. };
  79.  
  80. void display() {
  81. int i=0;
  82. int j = 0;
  83. for(i=0;i<SIZE;++i) {
  84. for(j = 0; j<SIZE; j++){
  85. if(dataList[i][j] != NULL)
  86. printf(" (%d,%d)",dataList[i][j]->key,dataList[i][j]->data);
  87. else printf("--");
  88. }
  89. printf("\n");
  90. }
  91. // printf("\n");
  92. }
  93.  
  94. int main() {
  95. singleItem = (struct DataItem*) malloc(sizeof(struct DataItem*));
  96. singleItem->data = -1;
  97. singleItem->key = -1;
  98.  
  99. insert(1,20);
  100. insert(2,70);
  101. insert(42,80);
  102. insert(4,25);
  103. insert(10,44);
  104. insert(14,32);
  105. insert(17,11);
  106. insert(20,78);
  107. insert(37,97);
  108. insert(37,53);
  109.  
  110. display();
  111. item = search(20);
  112. if(item!= NULL) {
  113. printf("Element found: %d\n",item->data);
  114. } else {
  115. printf("Element not found\n");
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment