散列函数线性探测法处理冲突:
#include <iostream>
using namespace std;
typedef int KeyType;
typedef int InfoType;
struct done
{
KeyType key;
InfoType otherinfo;
}HT[20];
int a[15];
void InsertHT(int x,int p)
{
int adr=x%p;
if(HT[adr].key==1000)
HT[adr].key=x;
else
{
adr=(adr+1)%p;
while(HT[adr].key!=1000)
{
adr=adr+1;
adr=adr%p;
}
HT[adr].key=x;
}
}
void CreateHT(int n,int m,int p)
{
for(int i=0;i<n;i++)
HT[i].key=1000;
for(int i=0;i<m;i++)
InsertHT(a[i],p);
}
void Display(int n,int m)
{
for(int i=0;i<n;i++)
cout<<i<<" ";
cout<<endl;
for(int i=0;i<m;i++)
cout<<HT[i].key<<" ";
cout<<"0 0"<<endl;
}
int main()
{
for(int i=0;i<13;i++)
{
cin>>a[i];
}
CreateHT(15,13,13);
Display(15,13);
return 0;
}