二次探测再散列散列表 源代码(数据结构)
2014-08-05 13:45
337 查看
二次探测再散列
/**********散列表**********/ #ifndef HashTable_ #define HashTable_ #include "Except.h" template <class E,class K> class HashTable { public: HashTable(int divisor = 11); ~HashTable(){delete [] ht;delete []empty;} bool Search(const K& k,E& e); HashTable<E,K>& Insert(const E& e); HashTable<E,K>& Delete(const K& k,E& e); void Output() const; private: int hSearch(const K& k)const; int D;//散列函数的除数 E* ht;//散列数组 bool *empty;//一维数组 }; template <class E,class K> HashTable<E,K> ::HashTable(int divisor) { //构造函数 D = divisor; //分配散列数组 ht = new E[D]; empty = new bool[D]; //将所有空桶置零 for(int i = 0;i < D;i++) { empty[i] = true; } } template<class E,class K> int HashTable<E,K>::hSearch(const K& k) const { int i = k % D; // 起始桶 int j = i;// 在起始桶处开始 int n = 1,count=0; do { if (empty[j] || ht[j] == k) return j; if( count == 0) { j = (i + n*n) % D; count = 1; } else { j = ((i - n*n) % D + D) % D; count = 0; n++; } } while (j !=i); // 返回起始桶 return j; // 表已满 } template<class E, class K> bool HashTable<E,K>::Search(const K& k, E& e) { int b = hSearch(k); if(empty[b] || ht[b] != k) return false; e = ht[b]; return true; } template<class E, class K> HashTable<E,K>& HashTable<E,K>::Insert(const E& e) { K k = e; int b = hSearch(k); if(empty[b] ) { empty[b] = false; ht[b] = e; return *this; } if(ht[b] == k) throw BadInput(); throw NoMem(); return *this; } template<class E, class K> HashTable<E,K>& HashTable<E,K>::Delete(const K& k,E& e) { int b = hSearch(k); if(empty[b] || ht[b] != k) throw OutOfBounds(); e = ht[b]; empty[b] = true; return *this; } template<class E, class K> void HashTable<E,K>::Output() const { for (int i = 0; i < D; i++) if(empty[i]) cout << "NULL" << " "; else cout << ht[i] << " "; } #endif //------------------------------------------ //.cpp #include <iostream> using namespace std; #include "HashTable.h" class element { friend int main(); public: operator long() const {return key;} /*bool operator < (const element other) const{ return (this->key < other.key);//translator element < element to K <K } bool operator == (const element other) const{ return (this->key == other.key); } bool operator < (const K other) const{ return (this->key < other);//translator elment < K to K < K } bool operator == (const K other) const{ return (this->key == other); } bool operator != (const K other) const{ return (this->key != other); } int operator % (int other) const{//support ht[e % P], P is int return (this->key % other); }*/ element& operator =(long y) {key = y; return *this;} private: int data; long key; }; HashTable<element, int> h(11); element e; int main() { e.key = 19; h.Insert(e); e.key = 01; h.Insert(e); e.key = 23; h.Insert(e); e.key = 14; h.Insert(e); e.key = 55; h.Insert(e); e.key = 68; h.Insert(e); e.key = 11; h.Insert(e); e.key = 82; h.Insert(e); e.key = 36; h.Insert(e); e.key = 12; h.Insert(e); h.Output(); cout << endl; element x; cout<<"查找元素:"<<endl; if(h.Search(1,x)) cout<< "位置:"<< x <<endl; else cout<<"元素不存在。"<< endl; cout << endl; e.key = 34; cout<<"插入:"<<endl; h.Insert(e); h.Output(); cout<<endl; return 0;
相关文章推荐
- 详细图解什么叫平方探查法即二次探测再散列和线性探测再散列(数据结构 哈希函数 哈希冲突)
- hash,分离链接法,探测散列表(二次探测),再散列的代码
- 散列表(四):冲突处理的方法之开地址法(二次探测再散列的实现)
- 数据结构--解决散列冲突,平方探测法
- java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区
- 散列表(四):冲突处理的方法之开地址法(二次探测再散列的实现)
- PAT-1078 Hashing (散列表 二次探测法)
- 散列表(四)冲突处理的方法之开地址法: 二次探测再散列的实现
- 哈希冲突的处理【闭散列方法-线性探测和二次探测】
- java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区
- 线性探测再散列和平方探测再散列(二次探测再散列)算法
- 哈希冲突的处理【闭散列方法-线性探测和二次探测】
- 数据结构:散列2(探测法)
- 共同学习Java源代码--数据结构--List接口
- 旭说数据结构之散列表(哈希表)
- 散列表(三):冲突处理的方法之开地址法(线性探测再散列的实现)
- HashTable二次探测
- 处理散列冲突的方法 - 数据结构和算法85
- 数据结构:JavaScript实现散列
- c语言实现数据结构顺序表源代码