Advertisement
Marisichka

Untitled

Oct 2nd, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7. class HashEntry {
  8. private:
  9.       int key;
  10.       int value;
  11.  
  12. public:
  13.       HashEntry(int key, int value) {
  14.             this->key = key;
  15.             this->value = value;
  16.       }
  17.  
  18.       int getKey() {
  19.             return key;
  20.       }
  21.  
  22.       int getValue() {
  23.         return value;
  24.       }
  25.       void delEl(){
  26.         delete this;
  27.       }
  28.  
  29. };
  30.  
  31. const int TABLE_SIZE = 50;
  32.  
  33. class HashMap {
  34. private:
  35.       HashEntry **table;
  36. public:
  37.       HashMap() {
  38.             table = new HashEntry*[TABLE_SIZE];
  39.             for (int i = 0; i < TABLE_SIZE; i++)
  40.                   table[i] = NULL;
  41.       }
  42.  
  43.       int get(int key) {
  44.             int hash = (key % TABLE_SIZE);
  45.             while (table[hash] != NULL && table[hash]->getKey() != key)
  46.                   hash = (hash + 1) % TABLE_SIZE;
  47.             if (table[hash] != NULL && table[hash]->getValue() != 0 )
  48.                   return table[hash]->getValue();
  49.  
  50.       }
  51.       void del_el(int key) {
  52.             int hash = (key % TABLE_SIZE);
  53.             if (table[hash]->getValue() % 2 == 0 ) {
  54.                     table[hash]=NULL;
  55.                     table[hash]->delEl();
  56.                     }
  57.             }
  58.       void put(int key, int value) {
  59.             int hash = (key % TABLE_SIZE);
  60.             while (table[hash] != NULL && table[hash]->getKey() != key)
  61.                   hash = (hash + 1) % TABLE_SIZE;
  62.             if (table[hash] != NULL)
  63.                   delete table[hash];
  64.             table[hash] = new HashEntry(key, value);
  65.       }
  66.  
  67.  
  68.  
  69.      /* ~HashMap() {
  70.             for (int i = 0; i < TABLE_SIZE; i++)
  71.                   if (table[i] != NULL)
  72.                         delete table[i];
  73.             delete[] table;
  74.       }*/
  75.  
  76. };
  77.  
  78. int main () {
  79.    
  80.     srand(time(NULL));
  81.     HashMap a;
  82.    
  83. for (int i=0;i<TABLE_SIZE;i++)
  84.     a.put(i,rand()%20+1);
  85. int c;
  86.  
  87. cout << "Generated Hash table:" << endl;
  88.  
  89. for (int i=0;i<TABLE_SIZE;i++) {
  90.     c = a.get(i);
  91.     cout << c << " ";
  92. }
  93.  
  94. cout << endl << "Hash table after deleting even elements:" << endl;
  95.  
  96. for (int i=0;i<TABLE_SIZE;i++)
  97.     a.del_el(i);
  98.    
  99. for (int i=0;i<TABLE_SIZE;i++) {
  100.     c=a.get(i);
  101.     cout << c << " ";
  102. }
  103. cout << endl << endl;
  104.  
  105. system("pause");
  106. return 0;
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement