Advertisement
Guest User

Untitled

a guest
May 28th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cstdlib>
  4.  
  5. struct Link {
  6.     Link(int value) {
  7.         this->value = value;
  8.         this->next = NULL;
  9.     }
  10.     int value;
  11.     Link* next;
  12. };
  13.  
  14. Link* hashTable[10000];
  15.  
  16. int getHashTableIndex(int key) { return abs(key) % 9721; }
  17.  
  18. bool existCmd(int key) {
  19.     int index = getHashTableIndex(key);
  20.  
  21.     if (!hashTable[index]) {
  22.         return false;
  23.     }
  24.     else {
  25.         Link *curLink = hashTable[index];
  26.         while (true) {
  27.             if (curLink->value == key) return true;
  28.  
  29.             if (!curLink->next) return false;
  30.             else curLink = curLink->next;
  31.         }
  32.     }
  33. }
  34.  
  35. void insertCmd(int key) {
  36.     if (existCmd(key)) return;
  37.  
  38.     int index = getHashTableIndex(key);
  39.  
  40.     if (!hashTable[index]) {
  41.         hashTable[index] = new Link(key);
  42.     }
  43.     else {
  44.         Link* curLink = hashTable[index];
  45.         while (curLink->next) curLink = curLink->next;
  46.         curLink->next = new Link(key);
  47.     }
  48. }
  49.  
  50.  
  51. void deleteCmd(int key) {
  52.     if (existCmd(key)) {
  53.         int index = getHashTableIndex(key);
  54.  
  55.         Link *curLink = hashTable[index];
  56.         if (curLink->value == key) {
  57.             hashTable[index] = curLink->next;
  58.             delete curLink;
  59.             return;
  60.         }
  61.  
  62.         while (true) {
  63.             Link* nextLink = curLink->next;
  64.             if (nextLink->value == key) {
  65.                 curLink->next = nextLink->next;
  66.                 delete nextLink;
  67.                 nextLink = NULL;
  68.                 return;
  69.             }
  70.             curLink = nextLink;
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76.  
  77. int main() {
  78.     std::ios_base::sync_with_stdio(0);
  79.  
  80.     freopen("/Users/Alex/ClionProjects/cpp_binary/input.txt", "r", stdin);
  81.     freopen("/Users/Alex/ClionProjects/cpp_binary/output.txt", "w", stdout);
  82.  
  83.     //freopen("set.in", "r", stdin);
  84.     //freopen("set.out", "w", stdout);
  85.  
  86.     int x;
  87.     std::string cmd;
  88.     while (std::cin >> cmd) {
  89.         std::cin >> x;
  90.         switch (cmd[0]) {
  91.             case 'i':
  92.                 insertCmd(x);
  93.                 break;
  94.             case 'd':
  95.                 deleteCmd(x);
  96.                 break;
  97.             case 'e':
  98.                 if (existCmd(x)) std::cout << "true\n";
  99.                 else std::cout << "fasle\n";
  100.                 break;
  101.         }
  102.     }
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement