作业帮 > 综合 > 作业

求高手帮忙14. 题目:哈希表查询设计及实现

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/07/07 12:55:59
求高手帮忙14. 题目:哈希表查询设计及实现
总体设计:设计哈希表,实现对英文单词的快速查找;
要求: (1)设计哈希表,该表应能够容纳50个英文单词。
(2)对该哈希表进行查询,实现对特定单词的快速查询,并显示经过的节点内容;百度已有的不给分,麻烦发邮箱!!谢谢了!!!发现可用不重复再追加100分!
/*
(1)设计哈希表,该表应能够容纳50个英文单词。
(2)对该哈希表进行查询,实现对特定单词的快速查询,并显示经过的节点内容
已经发到你邮箱里了 enochwills@hotmail.com
*/
#include
#include
#include
#include
#include
#define szNAME 80
#define HASH_ROOT 47 /*用于计算哈希地址的随机数*/
#define szHASH 50 /*哈希表总长度*/
#define POPULATION 30 /*学生总数*/
/*哈希表结构体*/
struct THash {
int key; /*钥匙码*/
char name[10]; /*姓名*/
int depth; /*检索深度*/
};
/*根据钥匙码和哈希根计算哈希地址*/
int GetHashAddress(int key, int root)
{
return key % root;
}/*end GetHashAddress*/
/*冲突地址计算,如果发现地址冲突,则用当前地址和钥匙码、哈希根重新生成一个新地址*/
int GetConflictAddress(int key, int address, int root)
{
int addr = address + key % 5 + 1;
return addr % root;
}/*end GetConflictAddress*/
/*根据字符串生成哈希钥匙码,这里的方法是将串内所有字符以数值形式求累加和*/
int CreateKey(char * name)
{
int key = 0;
unsigned char * n = (unsigned char *)name;
while(*n) key += *n++;
return key;
}/*end CreateKey*/
/*输入一个名字,并返回哈希钥匙码*/
int GetName(char * name)
{
scanf("%s", name);
return CreateKey(name);
}/*end CreateKey*/
/*根据学生人数、长度和哈希根构造哈希表*/
struct THash * CreateNames(int size, int root, int population)
{
int i =0, key = 0, addr = 0, depth = 0; char name[10];
struct THash * h = 0, *hash = 0;
/*哈希根和长度不能太小*/
if(size < root || root < 2) return 0;
/*根据哈希表长度构造一个空的哈希表*/
hash = (struct THash *)malloc(sizeof(struct THash) * size);
/*将整个表清空*/
memset(hash, 0, sizeof(struct THash) * size);
for(i = 0; i < population; i++) {
/*首先产生一个随机的学生姓名,并根据姓名计算哈希钥匙码,再根据钥匙码计算地址*/
key = GetName(name);
addr = GetHashAddress(key, root);
h = hash + addr;
if (h->depth == 0) { /*如果当前哈希地址没有被占用,则存入数据*/
h->key = key;
strcpy(h->name , name);
h->depth ++;
continue;
}/*end if*/
/*如果
很高兴回答楼主的问题 如有错误请见谅