Guest User

Untitled

a guest
Aug 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define EMPTY -1
  4. #define DELETED -2
  5. template<typename T1, typename T2>
  6. class Data {
  7. T1 a;
  8. T2 b;
  9. public:
  10. Data() {}
  11. Data (T1 t1, T2 t2) {
  12. a = t1;
  13. b = t2;
  14. }
  15.  
  16. T1 geta() { return a; }
  17.  
  18. void seta(T1 t1) { a = t1; }
  19.  
  20. T2 getb() { return b; }
  21.  
  22. void setb(T2 t2) { b = t2; }
  23.  
  24. bool operator == (Data<string, string> & temp){
  25. return (a == temp.geta() && b == temp.getb());
  26. }
  27. };
  28.  
  29. class LinearProbing {
  30. Data<int, string> * hashTable;
  31. int currentSize;
  32. const static int hashTableSize = 20;
  33. public:
  34. LinearProbing() {
  35. currentSize = 0;
  36. hashTable = new Data<int, string>[hashTableSize];
  37. for (int i = 0; i < hashTableSize; ++i) {
  38. hashTable[i].seta(EMPTY);
  39. }
  40. }
  41.  
  42. int hashFunction(int key) {
  43. return (key % hashTableSize);
  44. }
  45.  
  46. void insert(Data<int, string> myData) {
  47. if (isFull()) {
  48. return;
  49. }
  50.  
  51. int hashIndex = hashFunction(myData.geta());
  52. while(true) {
  53. if (hashTable[hashIndex].geta() == EMPTY) {
  54. hashTable[hashIndex] = myData;
  55. currentSize ++;
  56. break;
  57. }
  58. hashIndex = ((hashIndex + 1) % hashTableSize);
  59. }
  60. }
  61.  
  62. void deleteKeyValue(Data<int, string> myData) {
  63. if (isEmpty()) {
  64. return;
  65. }
  66.  
  67. int hashIndex = hashFunction(myData.geta());
  68. while(true) {
  69. if (hashTable[hashIndex].geta() == myData.geta() && hashTable[hashIndex].getb() == myData.getb()) {
  70. hashTable[hashIndex].seta(EMPTY);
  71. currentSize --;
  72. break;
  73. }
  74. hashIndex = ((hashIndex + 1) % hashTableSize);
  75. }
  76. }
  77.  
  78. bool isFull() { return (currentSize == hashTableSize); }
  79. bool isEmpty() { return (currentSize == 0); }
  80.  
  81. void display() {
  82. for (int i = 0; i < hashTableSize; ++i)
  83. {
  84. if (hashTable[i].geta() != EMPTY) {
  85. cout<<i<<" ("<<hashTable[i].geta()<<", "<<hashTable[i].getb()<<")\n";
  86. }
  87. else {
  88. cout<<i<<"\n";
  89. }
  90. }
  91. }
  92. };
  93. int main() {
  94. LinearProbing lp;
  95. pair<int, string> data[] = {make_pair(19, "Alpha"),
  96. make_pair(27, "Beta"),
  97. make_pair(36, "Gamma"),
  98. make_pair(10, "Delta"),
  99. make_pair(64, "Epsilon")};
  100. for (int i = 0; i < 5; ++i) {
  101. Data<int, string> temp(data[i].first, data[i].second);
  102. lp.insert(temp);
  103. }
  104. lp.display();
  105. return 0;
  106. }
Add Comment
Please, Sign In to add comment