Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <string>
- using namespace std;
- // Структура данных варианта
- struct BankAccount {
- int number;
- string name;
- string surname;
- string patronymic;
- string address;
- };
- // Вершина в списке
- template<typename T>
- struct Node {
- T* data;
- int key;
- Node* next;
- Node(T* data, int key) : data(data), key(key), next(nullptr) {}
- };
- // Класс хеш таблицы
- template<typename T>
- class HashTable {
- private:
- vector<Node<T>*>* table;
- int tableSize = 5;
- int itemsContaining = 0;
- public:
- // Конструктор
- HashTable() {
- table = new vector<Node<T>*>(tableSize, nullptr);
- }
- // Хэш функция
- int HashFunction(int key, int tableSize) {
- return key % tableSize;
- }
- // Добавление вершины в список
- void AddNode(Node<T>& node, T* data, int key) {
- if (!node.next) {
- node.next = new Node<T>(data, key);
- return;
- }
- AddNode(*node.next, data, key);
- }
- // Рехеширование
- void TableRehash() {
- int newTableSize = tableSize * 2;
- vector<Node<T>*>* newTable = new vector<Node<T>*>(newTableSize, nullptr);
- for (int i = 0; i < tableSize; ++i) {
- if (!table->operator[](i))
- continue;
- Node<T>* curNode = table->operator[](i);
- while (true) {
- int hashCode = HashFunction(curNode->key, newTableSize);
- if (!newTable->operator[](hashCode))
- newTable->operator[](hashCode) = new Node<T>(curNode->data, curNode->key);
- else
- AddNode(*newTable->operator[](hashCode), curNode->data, curNode->key);
- if (!curNode->next)
- break;
- curNode = curNode->next;
- }
- }
- table = newTable;
- tableSize = newTableSize;
- }
- // Добавление элемента
- void Add(T* data, int key) {
- int o = 0;
- int hashCode = HashFunction(key, tableSize);
- if (!table->operator[](hashCode))
- table->operator[](hashCode) = new Node<T>(data, key);
- else
- AddNode(*table->operator[](hashCode), data, key);
- itemsContaining++;
- if ((double)itemsContaining / (double)tableSize > 0.75) {
- TableRehash();
- }
- }
- // Возврат таблицы
- vector<T*> GetTable() {
- vector<T*> plainTable;
- for (int i = 0; i < tableSize; ++i) {
- if (!table->operator[](i))
- continue;
- Node<T>* curNode = table->operator[](i);
- while (true) {
- plainTable.push_back(curNode->data);
- if (!curNode->next)
- break;
- curNode = curNode->next;
- }
- }
- return plainTable;
- }
- // Возврат элемента по ключу
- T* Get(int key){
- int hashCode = HashFunction(key, tableSize);
- Node<T>* curNode = table->operator[](hashCode);
- if(!curNode) return nullptr;
- while (true) {
- if(curNode->key == key)
- return curNode->data;
- if (!curNode->next)
- return nullptr;
- curNode = curNode->next;
- }
- }
- // Удаление элементов
- void Delete(int key){
- int hashCode = HashFunction(key, tableSize);
- Node<T>* curNode = table->operator[](hashCode);
- if(!curNode)
- return;
- if(!curNode->next && curNode->key == key) {
- delete curNode;
- table->operator[](hashCode) = nullptr;
- return;
- }
- if(curNode->key == key && curNode->next){
- table->operator[](hashCode) = curNode->next;
- delete curNode;
- return;
- }
- while (true) {
- if(curNode->next && curNode->next->key == key){
- if(curNode->next->next){
- delete curNode->next;
- curNode->next = curNode->next->next;
- return;
- }
- if(!curNode->next->next){
- delete curNode->next;
- curNode->next = nullptr;
- return;
- }
- }
- if (!curNode->next)
- return;
- curNode = curNode->next;
- }
- }
- };
- int main() {
- HashTable<BankAccount>* a = new HashTable<BankAccount>();
- int number;
- string name;
- string surname;
- string patronymic;
- string address;
- int command = 0;
- int command_argument = 0;
- BankAccount* ba;
- vector<BankAccount*> b;
- cout << "Enter commands \n(\n\"0 [number of elements to add]\" - add elements\n" <<
- "\"1 [key]\" - get element by key\n\"2 [key]\" - delete element by key\n\"3\" - print full table\n\"4\" - exit\n)\n";
- while(true){
- cout << ">";
- cin >> command;
- switch (command) {
- case 0:
- cin >> command_argument;
- cout << "Enter " << command_argument << " elements\n";
- for(int i = 0; i < command_argument; ++i){
- cin >> number >> name >> surname >> patronymic >> address;
- ba = new BankAccount();
- ba->number = number;
- ba->name = name;
- ba->surname = surname;
- ba->patronymic = patronymic;
- ba->address = address;
- a->Add(ba, number);
- }
- break;
- case 1:
- cin >> command_argument;
- ba = a->Get(command_argument);
- if(ba)
- cout << ba->number << " " << ba->name << " " << ba->surname << " " << ba->patronymic << " " << ba->address << '\n';
- else
- cout << "No element by key " << command_argument << '\n';
- break;
- case 2:
- cin >> command_argument;
- a->Delete(command_argument);
- cout << "Element deleted\n";
- break;
- case 3:
- b = a->GetTable();
- for (int i = 0; i < b.size(); ++i) {
- cout << b[i]->number << " " << b[i]->name << " " << b[i]->surname << " " << b[i]->patronymic << " " << b[i]->address << '\n';
- }
- break;
- case 4:
- cout << "Exiting...";
- return 0;
- default:
- cout << "Unknown command\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment