Advertisement
aniksajli

HashingWithStruct

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