#define DefaultSize 50 #include<iostream> #include<cstdlib> #include<ctime> using namespace std; enum Status{Empty,Full}; template<class T> struct HashNode{ T data; Status st; HashNode<T>(){} HashNode<T>(T d ,Status s):data(d),st(s){} }; template<class T> class Hash{ //数组实现的hash表 private: HashNode<T> *hashtable; //哈希表 int Size; float a; //装载因子 int (*ptr[2])(T d); //指向散列函数的函数指针数组,若应用了二次探查法则有两个指针,若未应用则第二个指针为空 public: Hash(int sz,T data[],int len,int (*pf)(T d)); Hash(int sz,T data[],int len, int (*pf1)(T d),int (*pf2)(T d)); ~Hash(){delete hashtable;} bool Insert(T d); //元素插入散列表,线性探查法解决冲突 bool Delete(T &d); //元素从散列表中删除,线性探查法解决冲突 float yinzi(){return a;} //返回装载因子 int getSize(){return Size;} //返回哈希表长度 void output(){ cout << "输出哈希表:"; for(int i = 0; i < Size; i++) if(hashtable[i].st == Full)cout << hashtable[i].data << " "; else cout << "- "; cout << endl; } }; template<class T> Hash<T>::Hash(int sz,T data[],int len,int (*pf)(T d)){ int index; Size = sz; a = len/Size; ptr[0] = pf;ptr[1] = NULL; hashtable = new HashNode<T>[Size]; if(hashtable == NULL){ cout << "存储分配出错!" << endl; return; } if(Size < len){ cout << "在非拉链法的哈希表中哈希表的长度不能小于数据集合的长度!" << endl; return; } for(int i = 0; i < sz; i++) hashtable[i].st = Empty; for(int i = 0; i < len; i++){ index = (*pf)(data[i])%Size; //cout << "index:" << index << endl; if(hashtable[index].st == Empty){ hashtable[index].data = data[i]; hashtable[index].st = Full; } else{ int curpos = (index+1)%Size; while(hashtable[curpos].st == Full && curpos != index)curpos = (curpos+1)%Size; if(curpos == index){ cout << "表满,建哈希表失败!" << endl;return; } hashtable[curpos].data = data[i]; hashtable[curpos].st = Full; } } } template<class T> Hash<T>::Hash(int sz,T data[],int len, int (*pf1)(T key),int (*pf2)(T key)){ Size = sz; a = len/Size; ptr[0] = pf1;ptr[1] = pf2; hashtable = new HashNode<T>[Size]; int index; if(Size < len){ cout << "在非拉链法的哈希表中哈希表的长度不能小于数据集合的长度!" << endl; return; } for(int i = 0; i < Size; i++)hashtable[i].st = Empty; for(int i = 0; i < len; i++){ index =((*pf1)(data[i]) + (*pf2)(data[i])) % Size; //cout << "index:" << index << endl; if(hashtable[index].st != Empty){ int num = 0,index_temp; while(hashtable[index].st != Empty && num < 50){ num++; index_temp = (i+num)%len; index = ((*pf1)(data[index_temp]) + (*pf2)(data[index_temp])) % Size; } if(num == 50){ cout << "重复搜索" << index << "次数到达上限!" << endl; return; } } hashtable[index].data = data[i]; hashtable[index].st = Full; } } template<class T> bool Hash<T>::Insert(T d){ int index; if(ptr[1] != NULL){ index = ((*ptr[0])(d) + (*ptr[1])(d)) % Size; } else index = (*ptr[0])(d) % Size; if(hashtable[index].st == Empty){ hashtable[index].data = d; hashtable[index].st = Full; return true; } int curpos = (index + 1) % Size; while(hashtable[curpos].st != Empty && curpos != index){ curpos = (curpos + 1) % Size; } if(curpos == index){ cout << "无位置插入!" << endl; return false; } else{ hashtable[curpos].data = d; hashtable[curpos].st = Full; return true; } } template<class T> bool Hash<T>::Delete(T &d){ int index; if(ptr[1] != NULL){ index = ((*ptr[0])(d) + (*ptr[1])(d)) % Size; } else index = (*ptr[0])(d) % Size; if(hashtable[index].data == d){ hashtable[index].st = Empty; return true; } int curpos = (index + 1) % Size; while(hashtable[curpos].st == Full && hashtable[curpos].data != d && curpos != index){ curpos = (curpos + 1) % Size; } if(curpos == index || hashtable[curpos].st == Empty){ cout << "删除失败" << endl; return false; } hashtable[curpos].st = Empty; d = hashtable[curpos].data; return true; } int f(int a){ //返回哈希函数的值 return a*2; } int f1(int a){ return a+4; } double random(int a,int b){ return (abs(a-b)*rand()/(RAND_MAX+1.0)); } int main(){ int data[10];srand(unsigned(time(0))); for(int i = 0; i < 10; i++){ //产生数组的内容 data[i] = int(random(0,20)); } Hash<int> H(20,data,10,f); H.output(); Hash<int> H1(20,data,10,f,f1); H1.output(); H1.Insert(data[3]); H1.output(); H1.Delete(data[3]); H1.output(); return 0; }