哈希表(只存Key值)简易版
2016-03-20 13:44
204 查看
哈希表,存储key节点,用一个状态数组对应标识数据有效性(如果将标识和目标值放在结构体中不利于管理,暴露太多内部设计),该程序实现了查找表中是否包含目标值的功能。
#include<iostream> using namespace std; enum Status { EXIST, EMPTY, DELETE }; template<class K> class HashTable { public: HashTable(int capacity) :_table(new K[capacity]) , _capacity(capacity) , _size(0) , _statu(new Status[capacity]) { memset(_table, 0, sizeof(K)*capacity);//按字节初始化,不能这样初始化_statu for (int i = 0; i < capacity; ++i) { _statu[i] = EMPTY; } } HashTable(const HashTable<K>& hb) :_table(new K[hb._capacity]) , _capacity(hb._capacity) , _size(0) , _statu(new Status[_capacity]) { memset(_table, 0, sizeof(K)*_capacity); for (int i = 0; i < _capacity; ++i) { _statu[i] = EMPTY; } for (int i = 0; i < _capacity; ++i) { if (hb._statu[i] == EXIST) { this->Insert(hb._table[i]); } } } HashTable<K>& operator=(HashTable<K> ht) { this->_Swap(ht); return *this; } ~HashTable() { if (_table) delete[] _table; if (_statu) delete[] _statu; _size = 0; _capacity = 0; } bool Insert(const K& key) { if (_size * 10 / _capacity >= 7)//负载因子最好在0.7到0.8 { size_t newCapacity = _capacity * 2; HashTable<K> tmp(newCapacity); for (int i = 0; i < _capacity; ++i) { if (_statu[i] == EXIST) { tmp.Insert(_table[i]); } } this->_Swap(tmp);//自定义 } size_t index = _HashFunc(key); size_t begin = index; do { if (_statu[index] == EMPTY||_statu[index]==DELETE) break; if (_statu[index] == EXIST&&_table[index] == key) return true; ++index; if (index == _capacity) index = 0; } while (index != begin); if (_statu[index]==EXIST) return false; _table[index] = key; _statu[index] = EXIST; ++_size; return true; } bool Find(const K& key) { size_t index = _HashFunc(key); size_t begin = index; do { if (_statu[index] == EMPTY) break; if (_statu[index] == EXIST&&_table[index] == key) return true; ++index; if (index == _capacity) index = 0; } while (index != begin); return false; } //懒删除 bool Remove(const K& key) { size_t index = _HashFunc(key); size_t begin = index; do { if (_statu[index] == EMPTY) break; if (_statu[index] == EXIST&&_table[index] == key) { _statu[index] = DELETE; --_size; return true; } ++index; if (index == _capacity) index = 0; } while (index != begin); return false; } void Print() { for (int i = 0; i < _capacity; ++i) { printf("%d:%d-->", _table[i], _statu[i]); } cout << endl; } protected: size_t _HashFunc(const K& key) { return key%_capacity;//除留余数法 } void _Swap(HashTable<K>& hs) { swap(_table, hs._table); swap(_capacity, hs._capacity); swap(_size, hs._size); swap(_statu, hs._statu); } protected: K* _table; size_t _capacity; size_t _size; Status* _statu; }; void Test1() { HashTable<int> hs(10); hs.Insert(1); hs.Insert(11); hs.Insert(2); hs.Insert(3); hs.Insert(6); hs.Insert(9); hs.Insert(12); hs.Insert(8); hs.Insert(1); hs.Print(); cout << "Find(1)" << hs.Find(1) << "Find(7)" << hs.Find(7) << "Find(11)" << hs.Find(11) << endl; cout << "Remove(11)" << hs.Remove(11) << "Remove(0)" << hs.Remove(0) << endl; cout << "Find(11)" << hs.Find(11) << endl; } void Test2() { HashTable<int> ht(10); ht.Insert(89); ht.Insert(18); ht.Insert(49); ht.Insert(58); ht.Insert(9); ht.Print(); HashTable<int> hb(10); hb.Insert(1); hb.Insert(2); hb.Insert(3); hb.Insert(4); hb.Insert(9); hb.Print(); HashTable<int> h(ht); h.Print(); h = hb; h.Print(); }
相关文章推荐
- Windows Powershell使用哈希表
- 探索PowerShell (八) 数组、哈希表(附:复制粘贴技巧)
- 轻松学习C#的哈希表
- PHP内核探索:哈希表碰撞攻击原理
- php内核解析:PHP中的哈希表
- java中哈希表及其应用详解
- C#中HashTable的用法
- 哈希函数
- Qt中QSet的使用
- 数据结构-散列表(Hash Table)的C++实现模板
- 数据结构——哈希表
- Two Sum,3Sum,3Sum Closest,4Sum
- Java-Majority Element
- 常用算法C#实现:字符串包含问题
- 探测法的哈希表的C++实现(最新修改)
- [leetcode] Contains Duplicate 判断一数组是否有重复元素
- Zobrist哈希,研究棋类博弈必须了解的一个小工具
- LeetCode290 Word Pattern
- 哈希表单词计数
- Matlab杂记之 HashTable