数据结构 hashtable C++实现 链式
2015-03-07 00:00
435 查看
#include <iostream> #include <vector> #include <list> #include <algorithm> #include <iterator> bool isPrime(int x) { for(int i = 2; i * i <= x; ++i) { if(x % i == 0) return false; } return true; } int nextprime(int x) { for(;;++x) { if(isPrime(x)) return x; } } using namespace std; template<class HashedObj> class HashTable { public: explicit HashTable(int size = 101); bool contains(const HashedObj& x) const; void makeEmpty(); bool insert(const HashedObj& x); bool remove(const HashedObj& x); void printtab() const; private: vector<list<HashedObj> > theLists; int currentSize; void rehash(); int myhash(const HashedObj& x) const; int hash(const HashedObj& x) const; }; template<class HashedObj> HashTable<HashedObj>::HashTable(int size) { theLists.resize(size); } template<class HashedObj> bool HashTable<HashedObj>::contains(const HashedObj& x) const { const list<HashedObj>& whichList = theLists[myhash(x)]; return find(whichList.begin(), whichList.end(), x) == whichList.end(); } template<class HashedObj> void HashTable<HashedObj>::makeEmpty() { for(size_t i = 0; i < theLists.size(); ++i) theLists[i].clear(); } template<class HashedObj> bool HashTable<HashedObj>::insert(const HashedObj& x) { list<HashedObj> &whichList = theLists[myhash(x)]; if(find(whichList.begin(), whichList.end(), x) != whichList.end()) return false; whichList.push_back(x); if(++currentSize > theLists.size()) rehash(); return true; } template<class HashedObj> bool HashTable<HashedObj>::remove(const HashedObj& x) { list<HashedObj> &whichList = theLists[myhash(x)]; typename::list<HashedObj>::iterator itr = find(whichList.begin(), whichList.end(), x); if(itr == whichList.end()) return false; whichList.erase(itr); --currentSize; return true; } template<class HashedObj> void HashTable<HashedObj>::rehash() { vector<list<HashedObj> > oldLists = theLists; theLists.resize(nextprime(2 * theLists.size())); for(size_t j = 0; j < theLists.size(); ++j) theLists[j].clear(); currentSize = 0; for(size_t i = 0; i < oldLists.size(); ++i) { typename list<HashedObj>::iterator itr = oldLists[i].begin(); while(itr != oldLists[i].end()) insert(*itr++); } } template<class HashedObj> int HashTable<HashedObj>::myhash(const HashedObj& x) const { int hashval = hash(x); hashval %= theLists.size(); if(hashval < 0) hashval += theLists.size(); return hashval; } template<class HashedObj> void HashTable<HashedObj>::printtab() const { for(size_t i = 0; i < theLists.size(); ++i) { copy(theLists[i].begin(), theLists[i].end(), ostream_iterator<HashedObj>(cout, " ")); if(!theLists[i].empty()) cout << endl; } } template<> int HashTable<int>::hash(const int& x) const { return x % theLists.size(); } template<> int HashTable<string>::hash(const string& x) const { return x[0] % theLists.size(); } int main() { return 0; }
相关文章推荐
- 数据结构 hashtable C++实现 链式
- 数据结构 线性表 链式 c++源代码 实现
- 数据结构队列C++实现 链式和循环
- 数据结构学习之堆栈的链式存储C++实现
- 【数据结构】c++实现HashTable(开链法)
- c++实现数据结构八 链式队列
- 数据结构学习之队列的链式存储的C++实现
- 数据结构之C++实现链式队列(LinkQueue)(无主函数)
- 【数据结构】c++实现HashTable(开链法)
- 数据结构队列C++实现 链式和循环
- [C++]数据结构:散列表HashTable的实现与简单应用
- c/c++ 数据结构-线性表(链式表示&实现&单链表定义&实现)
- 数据结构学习系列二-链表的C++实现
- 算法与数据结构课程中的c++实现的顺序表和链表
- 数据结构各种算法实现C++
- 数据结构_树_二叉树的建立、遍历、复制与移除_二叉链表存储_C++实现
- 数据结构_串_用链表做存储结构实现KMP算法_C++实现
- 数据结构_图_邻接多重表做存储结构遍历无向图_C++实现
- 数据结构_链表_多项式求导_C++实现
- 数据结构_链表_稀疏多项式求值_C++实现