Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <time.h>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. class LinearProbing
  9. {
  10. private:
  11.     int *table;
  12. public:
  13.     LinearProbing();
  14.     int hashing(int key);
  15.     bool insert(int key);
  16.     void insertX(int X);
  17.     bool find(int key);
  18.     bool remove(int key);
  19.     void showFromTo(int from, int to);
  20. };
  21.  
  22. LinearProbing::LinearProbing()
  23. {
  24.     table = new int[997];
  25.     for(int i = 0; i < 997; i++)
  26.     {
  27.         table[i] = 0;
  28.     }
  29. }
  30. int LinearProbing::hashing(int key)
  31. {
  32.     return (((key % 1000) + int(pow(2,key % 10)) + 1) % 997);
  33. }
  34. bool LinearProbing::insert(int key)
  35. {
  36.     int index = hashing(key);
  37.     if(table[index] <= 0)
  38.     {
  39.         table[index] = key;
  40.         //cout<<key<<endl;
  41.        // cout<<index<<endl;
  42.         return true;
  43.     }
  44.     else
  45.     {
  46.         int next = index + 1;
  47.         while(next != 997)
  48.         {
  49.             if(table[next] <= 0)
  50.             {
  51.                 table[next] = key;
  52.                 return true;
  53.             }
  54.             next++;
  55.         }
  56.         next = 0;
  57.         while(next != index)
  58.         {
  59.             if(table[next] <= 0)
  60.             {
  61.                 table[next] = key;
  62.                 return true;
  63.             }
  64.             next++;
  65.         }
  66.     }
  67.     return false;
  68. }
  69. void LinearProbing::insertX(int X)
  70. {
  71.     int key = 0;
  72.     for(int i = 0; i < X; i++)
  73.     {
  74.         key = (rand()%20000)+20000;
  75.         if(insert(key) == false)
  76.             i--;
  77.         //insert(key);
  78.         //cout<<i<<")"<<table[i]<<"\t"<<endl;
  79.     }
  80. }
  81. bool LinearProbing::remove(int key)
  82. {
  83.     int index = hashing(key);
  84.     if (table[index] == key)
  85.     {
  86.         table[index] = -1;
  87.         return true;
  88.     }
  89.     else
  90.     {
  91.         int next = index + 1;
  92.         while (next != 997)
  93.         {
  94.             if (table[next] == key)
  95.             {
  96.                 table[next] = -1;
  97.                 return true;
  98.             }
  99.             next++;
  100.         }
  101.         next = 0;
  102.         while(next != index)
  103.         {
  104.             if (table[next] == key)
  105.             {
  106.                 table[next] = -1;
  107.                 return true;
  108.             }
  109.             next++;
  110.         }
  111.     }
  112.     return false;
  113. }
  114. bool LinearProbing::find(int key)
  115. {
  116.     int index = hashing(key);
  117.     if(table[index] == key)
  118.     {
  119.         return true;
  120.     }
  121.     else
  122.     {
  123.         int next = index + 1;
  124.         while(next != 997)
  125.         {
  126.             if(table[next] == key)
  127.             {
  128.                 return true;
  129.             }
  130.             next++;
  131.         }
  132.         next = 0;
  133.         while(next != index)
  134.         {
  135.             if(table[next] == key)
  136.             {
  137.                 return true;
  138.             }
  139.             next++;
  140.         }
  141.     }
  142.     return false;
  143. }
  144. void LinearProbing::showFromTo(int from, int to)
  145. {
  146.     if(from < 0 || to > 996)
  147.     {
  148.         cout<<"Wrong arguments! Choose from 0 to 996!"<<endl<<endl;
  149.         return;
  150.     }
  151.     else
  152.     {
  153.         for(int i = from; i <= to; i++)
  154.         {
  155.             cout<<i<<") "<<table[i]<<"\t";
  156.         }
  157.         cout<<endl;
  158.     }
  159. }
  160.  
  161. class DoubleHashing
  162. {
  163. private:
  164.     int tab
  165. };
  166.  
  167. int main()
  168. {
  169.     LinearProbing x;
  170.     x.showFromTo(0,5);
  171.     //x.insertX(10);
  172.     cout<<x.find(1)<<endl;
  173.     x.insert(12);
  174.     cout<<x.find(12)<<endl;
  175.     x.remove(12);
  176.     cout<<x.find(12)<<endl;
  177.     x.insert(12);
  178.     cout<<x.find(12)<<endl;
  179. /*
  180.     x.insert(11);
  181.     x.insert(112);
  182.     x.showFromTo(1,5);
  183.     cout<<x.hashing(12)<<endl;
  184.     cout<<x.hashing(11)<<endl;
  185.     cout<<x.hashing(112)<<endl;
  186. */
  187.  
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement