Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <cassert>
  5. #include <cmath>
  6. #define STUDENTS 3
  7.  
  8.  
  9. using namespace std;
  10.     class Hash
  11.     {
  12.         int BUCKET;
  13.         list<string>* table;
  14.        
  15.         public :
  16.             Hash(int V);
  17.             void insertItem (string x);
  18.             void deleteItem (string key);
  19.            
  20.            
  21.             int hashFunction(const std::string &str)
  22.                 {
  23.                     int hash = 0;
  24.                     for (auto &c: str) hash += c;
  25.                     return hash;
  26.                 }
  27.            
  28.            
  29.             void displayHash();
  30.            
  31.     };
  32.     Hash::Hash(int b)
  33.     {
  34.         this->BUCKET = b;
  35.         table = new list<string>[BUCKET];
  36.     }
  37.     void Hash::insertItem(std::string key)
  38.     {
  39.    
  40.        int index = hashFunction(key);
  41.         table[index].push_back(key);
  42.     }
  43.     void Hash::deleteItem(std::string key)
  44.     {
  45.         int index = hashFunction(key);
  46.         list<string> :: iterator i;
  47.         for (i = table[index].begin();i!=table[index].end();i++)
  48.         {
  49.             if(*i == key)
  50.             {
  51.                 break;
  52.             }
  53.            
  54.         }
  55.         if (i != table[index].end())
  56.         {
  57.             table[index].erase(i);
  58.         }
  59.      }
  60.         void Hash::displayHash()
  61.         {
  62.             for (int i =0; i < BUCKET;i++)
  63.             {
  64.                 cout << i;
  65.                 for (auto x: table[i])
  66.                 {
  67.                     cout << "----->" << x;
  68.                  
  69.                 }
  70.                 cout << endl;
  71.             }
  72.         }
  73.         int main ()
  74.         {
  75.         Hash h(STUDENTS);
  76.             for (int i =0;i<STUDENTS;i++)
  77.             {
  78.                 cout << "1.Add new student: " << endl;
  79.                 cout << "2.Delete student " << endl;
  80.                 cout << "3.Show hash-table " << endl;
  81.                 int switcher;
  82.                 cin >> switcher;
  83.                 switch(switcher)
  84.                 {
  85.                 case 1:
  86.                     {
  87.                     cin.ignore();
  88.                     cout << "Enter a value: " << endl;
  89.                    
  90.                     string value;
  91.                     getline(std::cin,value);
  92.                    
  93.                     h.insertItem(value);
  94.                     break;
  95.                     }
  96.                    
  97.                     case 2:
  98.                     {
  99.                     cin.ignore();
  100.                         cout << "Enter a value: " << endl;
  101.                        
  102.                         string value;
  103.                         getline(std::cin,value);
  104.                         h.deleteItem(value);
  105.                         break;
  106.                     }
  107.                     case 3:
  108.                     {
  109.                         h.displayHash();
  110.                         break;
  111.                     }
  112.                 }
  113.             }
  114.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement