【数据结构】c++实现HashTable(开链法)
来源:互联网 发布:打开telnet端口命令 编辑:程序博客网 时间:2024/06/08 09:03
#include <iostream>#include <vector>using namespace std;template <class K, class V>struct HashTableNode{K _key;V _value;HashTableNode<K, V>* _next;HashTableNode(const K&key, const V&value):_key(key), _value(value), _next(NULL){}};template <class K>struct DefaultHashFunc{size_t operator()(const K&key){return key;}};template <class K, class V, class HashFun = DefaultHashFunc<K>>class HashTableBucket{typedef HashTableNode<K, V> Node;public:HashTableBucket():_size(0){}HashTableBucket(size_t size):_size(0){_tables.resize(size);}bool Insert(const K&key, const V&value){_CheckExpand();size_t index = HashFunc(key, _tables.size());Node* begin = _tables[index];while (begin){if (begin->_key == key)return false;begin = begin->_next;}Node* tmp = new Node(key, value);tmp->_next = _tables[index];_tables[index] = tmp;++_size;return true;}size_t HashFunc(const K&key, size_t capacity){HashFun hf;return hf(key) % capacity;}void PrintTables(){for (size_t i = 0; i < _tables.size(); ++i){printf("Tables[%d]->",i);Node* cur = _tables[i];while (cur){cout << "[" << cur->_key << ":" << cur->_value << "]"<<"->";cur = cur->_next;}cout << "NULL"<<endl;}cout << endl;}protected:size_t _GetNextPrime(size_t size){static const int _PrimeSize = 28;static const unsigned long _PrimeList[_PrimeSize] ={53ul, 97ul, 193ul, 389ul, 769ul,1543ul, 3079ul, 6151ul, 12289ul, 24593ul,49157ul, 98317ul, 196613ul, 393241ul, 786433ul,1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,1610612741ul, 3221225473ul, 4294967291ul};for (size_t i = 0; i < _PrimeSize; ++i){if (_PrimeList[i] > size){return _PrimeList[i];}}return _PrimeList[_PrimeSize - 1];}void _CheckExpand(){// 载荷因子到1时进行增容,保证效率if (_size == _tables.size()){size_t newSize = _GetNextPrime(_size);if (_size == newSize)return;vector<Node*> newTables;newTables.resize(newSize);for (size_t i = 0; i < _tables.size(); ++i){Node* cur = _tables[i];while (cur){// 取节点Node* tmp = cur;cur = cur->_next;// 头插size_t newIndex = HashFunc(tmp->_key, newTables.size());tmp->_next = newTables[newIndex];newTables[newIndex] = tmp;}_tables[i] = NULL;}_tables.swap(newTables);}}protected:vector<HashTableNode<K, V>*> _tables;size_t _size;};
0 0
- 【数据结构】c++实现HashTable(开链法)
- c语言数据结构实现-哈希表/哈希桶(hashtable/hashbucket)
- [C++]数据结构:散列表HashTable的实现与简单应用
- 数据结构 hashtable C++实现 链式
- C语言实现 HashTable
- c语言实现hashtable
- C语言实现HashTable
- 数据结构是哈希表(hashTable)
- 数据结构是哈希表(hashTable)
- 【数据结构】:哈希表(hashtable)
- HashTable的c语言实现
- C 语言模仿实现HashTable
- 数据结构hashtable C++实现 非链式
- 【数据结构】HashTable原理及实现学习总结
- 简单的闭散列(hashtable)实现(c++)
- HashTable哈希表拉链法实现(C语言)
- 数据结构(C实现)------- 单链表
- 数据结构(C实现)------- 链栈
- bzoj4542【HNOI2016】大数
- Eclipse发布项目默认编译输出目录改为WEB-INF/classes
- 欢迎使用CSDN-markdown编辑器
- bzoj4514【SDOI2016】数字配对
- 科学计算工具-ipython
- 【数据结构】c++实现HashTable(开链法)
- bzoj4517【SDOI2016】排列计数
- 【Qt】自定义标题栏并实现鼠标拖拽移动
- php求素数的简单方法
- AOV网的拓扑排序
- Java---网络编程(4)-C/S-B/S
- Rendering Problems:Failed to convert .jpg in to a drawable
- HDOJ 1792 A New Change Problem (数论)
- MyBatis Generator 详解