脑图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200425164122449.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoaXl1a3VucGVuZw==,size_16,color_FFFFFF,t_70#pic_center)
代码
#include<iostream>
#include<cmath>
#include<stdlib.h>
#define MAXTABLESIZE 100000
#define NOTFIND -1
typedef int ElementType;
typedef enum{
Legitimate,Empty
} EntryType;
typedef struct HashEntry Cell;
struct HashEntry{
ElementType data;
EntryType info;
};
typedef struct HashTbl *HashTable;
struct HashTbl{
int TableSize;
Cell *Cells;
};
using namespace std;
int Hash(int key,int p){
return key%p;
}
int NextPrime(int N){
int p = N%2?N:N+1;
int i;
if(N<=2)
return 2;
else if(N<=3)
return 3;
while(p <= MAXTABLESIZE){
for(i=(int)sqrt(p);i>2;i--)
if(!(p%i))
break;
if(i==2)
break;
p += 2;
}
return p;
}
HashTable CreateTable(int TableSize){
HashTable H;
H = (HashTable)malloc(sizeof(struct HashTbl));
H->TableSize = NextPrime(TableSize);
H->Cells = (Cell *)malloc(sizeof(struct HashEntry)*H->TableSize);
for(int i=0;i<H->TableSize;i++)
H->Cells[i].info = Empty;
return H;
}
int Find(HashTable H,ElementType key){
int NewPos,CurrentPos;
int CNum = 0;
CurrentPos = NewPos = Hash(key,H->TableSize);
while(H->Cells[NewPos].info != Empty && H->Cells[NewPos].data != key){
CNum++;
NewPos = (CurrentPos + CNum*CNum)%H->TableSize;
if(CNum == H->TableSize/2)
return NOTFIND;
}
return NewPos;
}
int Insert(HashTable H,ElementType key){
int pos;
pos = Find(H,key);
if(pos==NOTFIND)
return NOTFIND;
else if(H->Cells[pos].info != Legitimate){
H->Cells[pos].info = Legitimate;
H->Cells[pos].data = key;
}
return pos;
}