Status SearchHash(HashTable H, int key, int *p, int *c) { int q; *c = 0;//重要!相当于全局变量,要重新赋初值 *p = _______________; //取得关键字的哈希地址 //若存储位置已有关键字(不等0),且关键字值与key不相等则 进行冲突处理 while (______________!= 0 && ______________!= key){ *p = Collision(_________, _________); } if (_________== key) return SUCCESS; else{ return UNSUCCESS; } }
时间: 2024-04-06 21:34:59 浏览: 22
Status SearchHash(HashTable H, int key, int *p, int *c) {
int q;
*c = 0; //重要!相当于全局变量,要重新赋初值
*p = Hash(key, H.size); //取得关键字的哈希地址
//若存储位置已有关键字(不等0),且关键字值与key不相等则进行冲突处理
while (H.elem[*p] != 0 && H.elem[*p] != key) {
*c += 1; //冲突次数加1
*p = Collision(*p, H.size); //线性探测法解决冲突
}
if (H.elem[*p] == key) return SUCCESS;
else {
return UNSUCCESS;
}
}
相关问题
Status InsertHash(HashTable *H, int key, int *p, int *c) { if (SearchHash(*H, key, p, c))//找不到插入新值 return ERROR; if (*c < H->length )//控制冲突次数 { _________= key; _________++; *c = 0;//重新初始化 return OK; } else { *c = 0; return ERROR; } }
Status InsertHash(HashTable *H, int key, int *p, int *c) {
if (SearchHash(*H, key, p, c)) //找不到插入新值
return ERROR;
if (*c < H->size) //控制冲突次数
{
H->elem[*p] = key;
H->count++; //元素个数加1
*c = 0; //重新初始化
return OK;
}
else {
*c = 0;
return ERROR;
}
}
C语言代码 【问题描述】 创建一个哈希表,构造方法使用“除留余数法”,并使用开放定址法来避免哈希冲突。编写查找函数完成以下功能: Status searchHash(HashTable hash, int key, int *addr) { } 【输入形式】 从键盘输入一个key值 【输出形式】 成功则返回该key值的哈希地址,或“查找失败” 【样例输入】 37 【样例输出】 查找37的哈希地址为:2
好的,以下是参考答案:
首先,我们需要先定义哈希表的结构体:
```c
#define HashSize 11 // 哈希表的大小
typedef int Status; // 函数返回状态,0表示失败,1表示成功
typedef int HashTable[HashSize]; // 哈希表数组
// 哈希表初始化
void InitHashTable(HashTable hash)
{
int i;
for (i = 0; i < HashSize; i++)
hash[i] = -1; // 初始化为-1
}
// 除留余数法
int Hash(int key)
{
return key % HashSize;
}
```
然后,我们需要实现开放定址法来解决哈希冲突:
```c
// 开放定址法解决哈希冲突
int Collision(int key, int i)
{
return i; // 线性探测
}
// 插入哈希表
Status InsertHash(HashTable hash, int key)
{
int addr = Hash(key); // 获取key的哈希地址
int i = 0;
// 如果该位置已经有元素,发生冲突
while (hash[addr] != -1 && hash[addr] != key)
{
i++;
addr = (Hash(key) + Collision(key, i)) % HashSize;
}
if (hash[addr] == -1) // 如果该位置为空,插入key
{
hash[addr] = key;
return 1;
}
else // 如果该位置已经有元素,插入失败
return 0;
}
```
最后,我们就可以实现查找函数了:
```c
// 查找函数
Status searchHash(HashTable hash, int key, int *addr)
{
int i = 0;
*addr = Hash(key); // 获取key的哈希地址
// 如果该位置为空,说明查找失败
while (hash[*addr] != -1 && hash[*addr] != key)
{
i++;
*addr = (Hash(key) + Collision(key, i)) % HashSize;
}
if (hash[*addr] == key) // 如果找到了key,返回成功
return 1;
else // 如果该位置为空,说明查找失败
return 0;
}
```
完整代码如下:
```c
#include <stdio.h>
#define HashSize 11 // 哈希表的大小
typedef int Status; // 函数返回状态,0表示失败,1表示成功
typedef int HashTable[HashSize]; // 哈希表数组
// 哈希表初始化
void InitHashTable(HashTable hash)
{
int i;
for (i = 0; i < HashSize; i++)
hash[i] = -1; // 初始化为-1
}
// 除留余数法
int Hash(int key)
{
return key % HashSize;
}
// 开放定址法解决哈希冲突
int Collision(int key, int i)
{
return i; // 线性探测
}
// 插入哈希表
Status InsertHash(HashTable hash, int key)
{
int addr = Hash(key); // 获取key的哈希地址
int i = 0;
// 如果该位置已经有元素,发生冲突
while (hash[addr] != -1 && hash[addr] != key)
{
i++;
addr = (Hash(key) + Collision(key, i)) % HashSize;
}
if (hash[addr] == -1) // 如果该位置为空,插入key
{
hash[addr] = key;
return 1;
}
else // 如果该位置已经有元素,插入失败
return 0;
}
// 查找函数
Status searchHash(HashTable hash, int key, int *addr)
{
int i = 0;
*addr = Hash(key); // 获取key的哈希地址
// 如果该位置为空,说明查找失败
while (hash[*addr] != -1 && hash[*addr] != key)
{
i++;
*addr = (Hash(key) + Collision(key, i)) % HashSize;
}
if (hash[*addr] == key) // 如果找到了key,返回成功
return 1;
else // 如果该位置为空,说明查找失败
return 0;
}
int main()
{
HashTable hash;
int key, addr;
InitHashTable(hash); // 初始化哈希表
// 插入一些元素
InsertHash(hash, 12);
InsertHash(hash, 25);
InsertHash(hash, 37);
InsertHash(hash, 54);
InsertHash(hash, 62);
InsertHash(hash, 77);
printf("请输入要查找的key值:");
scanf("%d", &key);
// 查找key的哈希地址
if (searchHash(hash, key, &addr))
printf("查找%d的哈希地址为:%d\n", key, addr);
else
printf("查找失败\n");
return 0;
}
```
输入37,输出结果为:
```
请输入要查找的key值:37
查找37的哈希地址为:2
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)