Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. ifstream input("set.in", ios_base::in);
  9. ofstream output("set.out", ios_base::out | ios_base::trunc);
  10.  
  11. struct Node {
  12.     Node() {
  13.     }
  14.  
  15.     Node(int value) {
  16.         this->value = value;
  17.     }
  18.  
  19.     int value;
  20.     Node *next;
  21. };
  22.  
  23. struct HashTable {
  24.     HashTable() {
  25.         hastTable.assign(SIZE + 3, nullptr);
  26.     }
  27.  
  28.     int getHash(int value) {
  29.         long a = value * 113 + 145861;
  30.         a %= 6130667;
  31.         a %= SIZE;
  32.         return abs(a);
  33.     }
  34.  
  35.     void insert(int value) {
  36.         if (!contains(value)) {
  37.             int hash = getHash(value);
  38.             Node *currNode = hastTable[hash];
  39.             if (currNode == nullptr) {
  40.                 hastTable[hash] = new Node(value);
  41.             } else {
  42.                 while (currNode->next != nullptr){
  43.                     currNode = currNode->next;
  44.                 }
  45.                 currNode->next = new Node(value);
  46.             }
  47.         }
  48.     }
  49.  
  50.     bool contains(int value) {
  51.         int hash = getHash(value);
  52.         Node *currNode = hastTable[hash];
  53.         while (currNode != nullptr && currNode->value != value) {
  54.             currNode = currNode->next;
  55.         }
  56.         return currNode != nullptr && currNode->value == value;
  57.     }
  58.  
  59.     void remove(int value) {
  60.         if (contains(value)) {
  61.             int hash = getHash(value);
  62.             Node *currNodePrev = hastTable[hash];
  63.             if (currNodePrev->next != nullptr) {
  64.                 Node *currNode = hastTable[hash]->next;
  65.                 while (currNode->value != value) {
  66.                     currNodePrev = currNodePrev->next;
  67.                     currNode = currNode->next;
  68.                 }
  69.                 if (currNode->next == nullptr) {
  70.                     currNodePrev->next = nullptr;
  71.                 } else {
  72.                     currNodePrev->next = currNode->next;
  73.                 }
  74.             } else {
  75.                 hastTable[hash] = nullptr;
  76.             }
  77.         }
  78.     }
  79.  
  80.     const int SIZE = 987911;
  81.     vector<Node *> hastTable;
  82. };
  83.  
  84. int main() {
  85.     string command;
  86.     int value;
  87.     HashTable hashtable;
  88.     while (input >> command) {
  89.         input >> value;
  90.         if (command == "insert") {
  91.             hashtable.insert(value);
  92.         } else if (command == "exists") {
  93.             output << (hashtable.contains(value) ? "true" : "false") << "\n";
  94.         } else if (command == "delete") {
  95.             hashtable.remove(value);
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement