2、编写程序,新建一个容量不小于50的哈希表(采用线性探测法解决冲突),随机生成30个整数插入哈希表中,整数范围在[0,1000]之间。(1)从键盘输入一个整数,在哈希表中查找,若找到,输出下标,否

题目:编写程序,新建一个容量不小于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;	
}

实验结果:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值