Advertisement
shchuko

5a

Nov 16th, 2019
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. #define IN_FILE_NAME "set.in"
  6. #define OUT_FILE_NAME "set.out"
  7.  
  8. class IntList {
  9. private:
  10.     class ListElement {
  11.     public:
  12.         int value = 0;
  13.         ListElement* p_prev = nullptr;
  14.         ListElement* p_next = nullptr;
  15.     };
  16.  
  17.     ListElement* p_begin = nullptr;
  18.     ListElement* p_end = nullptr;
  19.     long list_size = 0;
  20.  
  21.     ListElement* find(int _value);
  22.  
  23. public:
  24.     ~IntList();
  25.  
  26.     void pushBack(int _value);
  27.  
  28.     bool isExist(int _value);
  29.  
  30.     void remove(int _value);
  31.  
  32.     long size();
  33. };
  34.  
  35. class IntHashTable {
  36. private:
  37.     IntList* table = nullptr;
  38.     long table_size;
  39.     const double A = (sqrt(5) - 1) / 2;
  40.  
  41.     long hash(int key);
  42.  
  43. public:
  44.     explicit IntHashTable(long size);
  45.  
  46.     ~IntHashTable();
  47.  
  48.     void add(int key);
  49.  
  50.     bool isExist(int key);
  51.  
  52.     void remove(int key);
  53.  
  54. };
  55.  
  56. // IntList class methods
  57. IntList::ListElement *IntList::find(int _value) {
  58.     if (!list_size)
  59.         return nullptr;
  60.  
  61.     ListElement *p_look = p_begin;
  62.     while (p_look != nullptr && p_look->value != _value)
  63.         p_look = p_look->p_next;
  64.     return p_look;
  65. }
  66.  
  67. IntList::~IntList() {
  68.     ListElement* p_look = p_begin;
  69.     while (p_look != nullptr) {
  70.         ListElement* temp = p_look;
  71.         p_look = p_look->p_next;
  72.         delete(temp);
  73.     }
  74.  
  75.     p_begin = nullptr;
  76.     p_end = nullptr;
  77. }
  78.  
  79. void IntList::pushBack(int _value) {
  80.     list_size++;
  81.     auto* temp = new ListElement;
  82.     temp->value = _value;
  83.  
  84.     if (p_end == nullptr) {
  85.         p_begin = temp;
  86.         p_end = temp;
  87.         return;
  88.     }
  89.  
  90.     temp->p_prev = p_end;
  91.     p_end->p_next = temp;
  92.     p_end = temp;
  93.  
  94. }
  95.  
  96. bool IntList::isExist(int _value) {
  97.     return find(_value) != nullptr;
  98. }
  99.  
  100. void IntList::remove(int _value) {
  101.     ListElement* ptr_temp = find(_value);
  102.     if (ptr_temp == nullptr)
  103.         return;
  104.  
  105.     if (ptr_temp == p_begin)
  106.         p_begin = ptr_temp->p_next;
  107.     if (ptr_temp == p_end)
  108.         p_end = ptr_temp->p_prev;
  109.  
  110.     if (ptr_temp->p_prev != nullptr)
  111.         ptr_temp->p_prev->p_next = ptr_temp->p_next;
  112.     if (ptr_temp->p_next != nullptr)
  113.         ptr_temp->p_next->p_prev = ptr_temp->p_prev;
  114.  
  115.     delete(ptr_temp);
  116.     --list_size;
  117. }
  118.  
  119. long IntList::size() {
  120.     return list_size;
  121. }
  122.  
  123. // IntHashTable class methods
  124. long IntHashTable::hash(int key) {
  125.     if (key < 0)
  126.         key = -key;
  127.     double int_part;
  128.     return (long)(table_size * modf(key * A, &int_part));
  129. }
  130.  
  131. IntHashTable::IntHashTable(long size) {
  132.     table = new IntList[size];
  133.     table_size = size;
  134. }
  135.  
  136. IntHashTable::~IntHashTable() {
  137.     delete[](table);
  138.     table = nullptr;
  139. }
  140.  
  141. void IntHashTable::add(int key) {
  142.     long hash_value = hash(key);
  143.     if (!table[hash_value].isExist(key))
  144.         table[hash_value].pushBack(key);
  145. }
  146.  
  147. bool IntHashTable::isExist(int key) {
  148.     return table[hash(key)].isExist(key);
  149. }
  150.  
  151. void IntHashTable::remove(int key) {
  152.     table[hash(key)].remove(key);
  153. }
  154.  
  155. using std::ifstream;
  156. using std::ofstream;
  157. using std::string;
  158.  
  159. int main() {
  160.     ifstream fin(IN_FILE_NAME);
  161.     if (!fin.is_open())
  162.         return 2;
  163.  
  164.     ofstream fout(OUT_FILE_NAME);
  165.     if (!fout.is_open())
  166.         return 2;
  167.  
  168.     IntHashTable table(50000);
  169.     while (true) {
  170.         string command;
  171.         fin >> command;
  172.  
  173.         if (command.empty())
  174.             break;
  175.  
  176.         if (command == "insert") {
  177.             int value;
  178.             fin >> value;
  179.             table.add(value);
  180.         } else if (command == "delete") {
  181.             int value;
  182.             fin >> value;
  183.             table.remove(value);
  184.         } else if (command == "exists") {
  185.             int value;
  186.             fin >> value;
  187.             if (table.isExist(value))
  188.                 fout << "true\n";
  189.             else
  190.                 fout << "false\n";
  191.         }
  192.     }
  193.  
  194.     fin.close();
  195.     fout.close();
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement