二次探测再散列散列表 源代码(数据结构)

二次探测再散列 

/**********散列表**********/
#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;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值