散列表
i. 散列函数
i. 冲突解决
ii. 分离链表法
ii. 开放地址法
iii. 线性探测法
iii. 平方探测法
iii. 双散列
ii. 再散列
ii. 可扩散列
i. 装填因子:元素个数 / 散列表大小
C++实现
1. 散列函数:key % tableSize
冲突解决:分离链表法
/* 散列函数: key % tableSize 冲突解决: 分离链表法 */ #include <iostream> #include <vector> #include <list> using namespace std; typedef int value; class HashTable { public: HashTable(unsigned size) : _size(size) { _table = new vector<list<value>* >(size); for (size_t i = 0; i < _size; i++) { (*_table)[i] = new list<value>; } } void insert(value val) { (*_table)[ val % _size ]->push_back(val); } value find(value val) { list<value>::const_iterator it = (*_table)[val % _size]->cbegin(); while (it != (*_table)[val % _size]->cend()) { if (*it == val) return *it; it++; } return NULL; } ~HashTable() { for (size_t i = 0; i < _size; i++) { delete (*_table)[i]; } delete _table; } private: vector<list<value>* >* _table; unsigned _size; }; int main() { HashTable h(11); h.insert(4); h.insert(4 + 11); cout << h.find(4 + 11); return 0; }
原文:http://www.cnblogs.com/fengyubo/p/5107511.html