Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <functional>
- #include <list>
- using namespace std;
- class HashTable {
- public:
- HashTable(int SIZE, function<int(int value)> hashFunction) {
- this->SIZE = SIZE;
- mainArray = new list<int> * [SIZE];
- for (int i = 0; i < SIZE; i++) {
- mainArray[i] = new list<int>;
- }
- this->hashFunction = hashFunction;
- }
- ~HashTable()
- {
- delete[] mainArray;
- }
- void Add(int value) {
- mainArray[hashFunction(value)]->push_back(value);
- }
- void Delete(int value) {
- int index = hashFunction(value);
- auto it = mainArray[index]->begin();
- for (auto it = mainArray[index]->begin(); it != mainArray[index]->end(); it++) {
- if (*it == value) {
- mainArray[index]->erase(it);
- break;
- }
- }
- }
- void Find(int value) {
- int index = hashFunction(value);
- auto it = mainArray[index]->begin();
- bool finded = false;
- int pos = 0;
- for (auto it = mainArray[index]->begin(); it != mainArray[index]->end(); it++) {
- if (*it == value) {
- finded = true;
- break;
- }
- pos++;
- }
- if (finded) {
- cout << "Данное значение хранится в " << hashFunction(value) << " индексе и на " << pos << " позиции" << endl;
- }
- else {
- cout << "Данное значение не найдено!" << endl;
- }
- }
- private:
- list<int>** mainArray;
- int SIZE;
- function<int(int value)> hashFunction;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement