题目:编写程序,新建一个容量不小于50的哈希表(采用线性探测法解决冲突),随机生成30个整数插入哈希表中,整数范围在[0,1000]之间。
(1)从键盘输入一个整数,在哈希表中查找,若找到,输出下标,否则输出“not found”。
(2)输出该哈希表当前的查找成功ASL。
知识点梳理:
1.哈希表的结构体:
enum EntryType {Legitimate, Empty, Deleted};
struct HashEntry {
int Data;
enum EntryType Info;
};
struct HashTable {
int TableSize;
struct HashEntry *Cells; /* 存放条目的数组 */
};
2.创建哈希表 :
void Insert(struct HashTable *H, int K) {
int Pos;
Pos = Find(H, K);
if (H->Cells[Pos].Info != Legitimate) {
H->Cells[Pos].Info = Legitimate;
H->Cells[Pos].Data = K;
}
}
3.除留余数法:
int Hash(int K, int TableSize) {
return K % TableSize;
}
4.向哈希表中进行插入:
void Insert(struct HashTable *H, int K) {
int Pos;
Pos = Find(H, K);
if (H->Cells[Pos].Info != Legitimate) {
H->Cells[Pos].Info = Legitimate;
H->Cells[Pos].Data = K;
}
}
5.用除留余数法查找是否存在K:
int Find(struct HashTable *H, int K) {
int Pos, NewPos;
int CNum = 0;
Pos = Hash(K, H->TableSize);//除留余数法
NewPos = Pos;
while (H->Cells[NewPos].Info != Empty
&& H->Cells[NewPos].Data != K ) {//该位置已被占用且保证不会重复插入
CNum ++;
NewPos = Pos + CNum;
if (NewPos >= H->TableSize)
NewPos %= H->TableSize;
}
return NewPos;
}
6.计算哈希ASL:
int FindX(struct HashTable *H, int K) {
int Pos, NewPos;
int CNum = 0;
Pos = Hash(K, H->TableSize);//除留余数法
NewPos = Pos;
while (H->Cells[NewPos].Info != Empty
&& H->Cells[NewPos].Data != K ) {//该位置已被占用且保证不会重复插入
CNum ++;
NewPos = Pos + CNum;
if (NewPos >= H->TableSize)
NewPos %= H->TableSize;
}
return CNum+1;
}
实验源码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int *p;
enum EntryType {Legitimate, Empty, Deleted};
struct HashEntry {
int Data;
enum EntryType Info;
};
struct HashTable {
int TableSize;
struct HashEntry *Cells; /* 存放条目的数组 */
};
//创建哈希表
struct HashTable *CreateTable(int TableSize) {
struct HashTable *H;
int i;
H = malloc(sizeof(struct HashTable));
H->TableSize=TableSize; /* 得到素数作为表大小 */
H->Cells = malloc(sizeof(struct HashEntry) * H->TableSize);
for (i = 0; i < H->TableSize; i ++)
H->Cells[i].Info = Empty;
return H;
}
//除留余数法
int Hash(int K, int TableSize) {
return K % TableSize;
}
//向哈希表中插入
void Insert(struct HashTable *H, int K) {
int Pos;
Pos = Find(H, K);
if (H->Cells[Pos].Info != Legitimate) {
H->Cells[Pos].Info = Legitimate;
H->Cells[Pos].Data = K;
}
}
//查找是否存在k
int Find(struct HashTable *H, int K) {
int Pos, NewPos;
int CNum = 0;
Pos = Hash(K, H->TableSize);//除留余数法
NewPos = Pos;
while (H->Cells[NewPos].Info != Empty
&& H->Cells[NewPos].Data != K ) {//该位置已被占用且保证不会重复插入
CNum ++;
NewPos = Pos + CNum;
if (NewPos >= H->TableSize)
NewPos %= H->TableSize;
}
return NewPos;
}
int FindX(struct HashTable *H, int K) {
int Pos, NewPos;
int CNum = 0;
Pos = Hash(K, H->TableSize);//除留余数法
NewPos = Pos;
while (H->Cells[NewPos].Info != Empty
&& H->Cells[NewPos].Data != K ) {//该位置已被占用且保证不会重复插入
CNum ++;
NewPos = Pos + CNum;
if (NewPos >= H->TableSize)
NewPos %= H->TableSize;
}
return CNum+1;
}
int main(){
struct HashTable *H;
H=CreateTable(100);
int i;
float sum=0;
srand((int)time(NULL));
for(i=0;i<30;i++){
int j,f;
f=rand()%1000+0;
Insert(H,f);
j=FindX(H,f);
sum=sum+j;
}
int m,k;
printf("请输入一个要查找的数:");
scanf("%d",&m);
k= Find(H,m);
printf("%d\n",k);
printf("%f",sum/30.0);
return 0;
}
实验结果: