From 07f34fe0bbd950a8964208e7d723a49308aa131c Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Tue, 5 Aug 2025 21:36:14 +0800 Subject: [PATCH] hashTable --- Hash/hashArray/hashTable.h | 81 ++++++++++++++++++++++++++++++++++++++ Hash/hashArray/main.cpp | 27 +++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 Hash/hashArray/hashTable.h create mode 100644 Hash/hashArray/main.cpp diff --git a/Hash/hashArray/hashTable.h b/Hash/hashArray/hashTable.h new file mode 100644 index 0000000..8ab3199 --- /dev/null +++ b/Hash/hashArray/hashTable.h @@ -0,0 +1,81 @@ +// +// Created by E2hang on 2025/8/5. +// + +#ifndef HASHTABLE_H +#define HASHTABLE_H +#include +#include + +template +class hashTable { +private: + std::hash hashFunc; + std::vector>> table; + int dsize; + int division; +public: + hashTable(int d = 1009); + ~hashTable() = default; + //hashTable(const hashTable&); + int searchj(const K &key); + int searchi(const K &key) {return hashFunc(key) % division;}; + void insert(const K& key, const E& e); + void erase(const K& key); + std::pair* find(const K& key); +}; + +template +hashTable::hashTable(int d) : dsize(0), division(d) { + table.resize(d); +} + +template +int hashTable::searchj(const K &key) { + int i = static_cast(hashFunc(key) % division); + int j = 0; + for (std::pair x : table[i]) { + if (x.first == key ) return j; + j++; + } + return -1; +} + +template +void hashTable::insert(const K &key, const E &e) { + int i = static_cast(hashFunc(key) % division); + for (auto &x : table[i]) { + if (x.first == key) { + x.second = e; + return; + } + } + table[i].push_back(std::make_pair(key, e)); +} + + +template +void hashTable::erase(const K &key) { + int i = static_cast(hashFunc(key) % division); + auto &bucket = table[i]; + for (auto it = bucket.begin(); it != bucket.end(); ++it) { + if (it->first == key) { + bucket.erase(it); + return; // 删除后退出 + } + } +} + + +template +std::pair* hashTable::find(const K &key) { + int i = static_cast(hashFunc(key) % division); + int j = searchj(key); + if (j == -1) { + return nullptr; // 找不到返回 nullptr + } + return &table[i][j]; +} + + +#endif //HASHTABLE_H diff --git a/Hash/hashArray/main.cpp b/Hash/hashArray/main.cpp new file mode 100644 index 0000000..77d1dbe --- /dev/null +++ b/Hash/hashArray/main.cpp @@ -0,0 +1,27 @@ +#include +#include "hashTable.h" +using namespace std; +int main() { + hashTable table; + table.insert("start", 0); + table.insert("first", 1); + table.insert("second", 2); + table.insert("third", 3); + table.insert("fourth", 4); + table.insert("fifth", 5); + table.insert("sixth", 6); + table.insert("seven", 7); + table.insert("eight", 8); + table.insert("nine", 9); + table.insert("ten", 10); + table.insert("eleven", 11); + table.insert("twelve", 12); + table.insert("Manbo", 13); + cout << table.searchi("first") << " " << table.searchj("first") << endl; + cout << table.searchi("second") << " " << table.searchj("second") << endl; + cout << table.searchi("third") << " " << table.searchj("third") << endl; + + cout << table.searchi("Manbo") << " "<< table.searchj("Manbo") << endl; + + return 0; +} \ No newline at end of file