Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <string>
- #include <cassert>
- #include <cmath>
- #define STUDENTS 3
- using namespace std;
- class Hash
- {
- int BUCKET;
- list<string>* table;
- public :
- Hash(int V);
- void insertItem (string x);
- void deleteItem (string key);
- int hashFunction(const std::string &str)
- {
- int hash = 0;
- for (auto &c: str) hash += c;
- return hash;
- }
- void displayHash();
- };
- Hash::Hash(int b)
- {
- this->BUCKET = b;
- table = new list<string>[BUCKET];
- }
- void Hash::insertItem(std::string key)
- {
- int index = hashFunction(key);
- table[index].push_back(key);
- }
- void Hash::deleteItem(std::string key)
- {
- int index = hashFunction(key);
- list<string> :: iterator i;
- for (i = table[index].begin();i!=table[index].end();i++)
- {
- if(*i == key)
- {
- break;
- }
- }
- if (i != table[index].end())
- {
- table[index].erase(i);
- }
- }
- void Hash::displayHash()
- {
- for (int i =0; i < BUCKET;i++)
- {
- cout << i;
- for (auto x: table[i])
- {
- cout << "----->" << x;
- }
- cout << endl;
- }
- }
- int main ()
- {
- Hash h(STUDENTS);
- for (int i =0;i<STUDENTS;i++)
- {
- cout << "1.Add new student: " << endl;
- cout << "2.Delete student " << endl;
- cout << "3.Show hash-table " << endl;
- int switcher;
- cin >> switcher;
- switch(switcher)
- {
- case 1:
- {
- cin.ignore();
- cout << "Enter a value: " << endl;
- string value;
- getline(std::cin,value);
- h.insertItem(value);
- break;
- }
- case 2:
- {
- cin.ignore();
- cout << "Enter a value: " << endl;
- string value;
- getline(std::cin,value);
- h.deleteItem(value);
- break;
- }
- case 3:
- {
- h.displayHash();
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement