Chaining Hash table
来源:互联网 发布:网络割接回退方案 编辑:程序博客网 时间:2024/04/26 08:15
#include <iostream>#include <Windows.h>#include <vector>using namespace std;const int TABLE_SIZE = 51;template <typename T>class LinkedHashEntry {private:int key;T value;LinkedHashEntry<T> *next;public:LinkedHashEntry(int key, int value) {this->key = key;this->value = value;this->next = NULL;}int getKey() {return key;}T getValue() {return value;}void setValue(const T &value) {this->value = value;}LinkedHashEntry *getNext() {return next;}void setNext(LinkedHashEntry<T> *next) {this->next = next;}};template <typename T>class HashMap {private:LinkedHashEntry<T> **table;public:HashMap() {table = new LinkedHashEntry<T>*[TABLE_SIZE];for (int i = 0; i < TABLE_SIZE; i++)table[i] = NULL;}T get(int key) {int hash = (key % TABLE_SIZE);if (table[hash] == NULL)return -1;else {LinkedHashEntry<T> *entry = table[hash];while (entry != NULL && entry->getKey() != key)entry = entry->getNext();if (entry == NULL)return -1;elsereturn entry->getValue();}}void put(int key, const T &value) {int hash = (key % TABLE_SIZE);if (table[hash] == NULL)table[hash] = new LinkedHashEntry<T>(key, value);else {LinkedHashEntry<T> *entry = table[hash];while (entry->getNext() != NULL && entry->getKey() != key)entry = entry->getNext();// exist entity with the same key value, so replace// no same key value, insertif (entry->getKey() == key)entry->setValue(value);elseentry->setNext(new LinkedHashEntry<T>(key, value));}}void remove(int key) {int hash = (key % TABLE_SIZE);if (table[hash] != NULL) {LinkedHashEntry<T> *prevEntry = NULL;LinkedHashEntry<T> *entry = table[hash];while (entry->getNext() != NULL && entry->getKey() != key) {prevEntry = entry;entry = entry->getNext();}if (entry->getKey() == key) {if (prevEntry == NULL) {LinkedHashEntry<T> *nextEntry = entry->getNext();delete entry;table[hash] = nextEntry;}else {LinkedHashEntry<T> *next = entry->getNext();delete entry;prevEntry->setNext(next);}}}}~HashMap() {for (int i = 0; i < TABLE_SIZE; i++)if (table[i] != NULL) {LinkedHashEntry<T> *prevEntry = NULL;LinkedHashEntry<T> *entry = table[i];while (entry != NULL) {prevEntry = entry;entry = entry->getNext();delete prevEntry;}}delete[] table;}};int main(){HashMap<int> hashtable;hashtable.put(10, 100);hashtable.put(61, 199);hashtable.put(1, 10);hashtable.put(52, 22);cout << hashtable.get(1) << endl;hashtable.put(1, 1001);cout << hashtable.get(1) << endl;hashtable.remove(1);cout << hashtable.get(1) << endl;system("PAUSE");return 0;}
The code is from:
http://www.algolist.net/Data_structures/Hash_table/Chaining
The top of chaining hash table is:
More proof and detail could be found at:
http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap12.htm
0 0
- Chaining Hash table
- 算法学习 - Hash Table (Separate Chaining)
- 链接法(chaining)构建散列表(hash table)(C++实现)
- 链接法(chaining)构建散列表(hash table)(C++实现)
- 《数据结构》学习-- Hash(2) --Separate Chaining
- Hash Table
- Hash Table
- Hash Table
- Hash Table
- HASH TABLE
- Hash Table
- Hash Table
- hash table
- Hash Table
- Hash Table
- hash table
- Hash Table
- hash table
- 手机端图片滑动切换效果
- 白平衡基础
- ibus输入法窗口位置异常左下角
- Linux-Bash技巧——字符串和base64互转
- 超声波引导系统开源(四)485软件网络架构设计
- Chaining Hash table
- python3数据类型简介
- HTML&CSS语法大全
- Http状态码与常见问题出现的原因
- Varnish的部署与使用实例(内附一键安装部署脚本github链接)
- AngularJS基本知识
- Android基础再回首——四大组件之Activity、Service俩兄弟
- SAPUI5 (10) - XMLView
- EM算法 估计混合高斯模型参数 Python实现