博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PTA线性探测法的查找函数c++版——山东科技大学
阅读量:4030 次
发布时间:2019-05-24

本文共 2650 字,大约阅读时间需要 8 分钟。

试实现线性探测法的查找函数。

函数接口定义:

Position Find( HashTable H, ElementType Key );

其中HashTable是开放地址散列表,定义如下:

#define MAXTABLESIZE 100000  /* 允许开辟的最大散列表长度 */typedef int ElementType;     /* 关键词类型用整型 */typedef int Index;           /* 散列地址类型 */typedef Index Position;      /* 数据所在位置与散列地址是同一类型 *//* 散列单元状态类型,分别对应:有合法元素、空单元、有已删除元素 */typedef enum {
Legitimate, Empty, Deleted } EntryType;typedef struct HashEntry Cell; /* 散列表单元类型 */struct HashEntry{
ElementType Data; /* 存放元素 */ EntryType Info; /* 单元状态 */};typedef struct TblNode *HashTable; /* 散列表类型 */struct TblNode {
/* 散列表结点定义 */ int TableSize; /* 表的最大长度 */ Cell *Cells; /* 存放散列单元数据的数组 */};

函数Find应根据裁判定义的散列函数Hash( Key, H->TableSize )从散列表H中查到Key的位置并返回。如果Key不存在,则返回线性探测法找到的第一个空单元的位置;若没有空单元,则返回ERROR。

裁判测试程序样例:

#include 
#define MAXTABLESIZE 100000 /* 允许开辟的最大散列表长度 */typedef int ElementType; /* 关键词类型用整型 */typedef int Index; /* 散列地址类型 */typedef Index Position; /* 数据所在位置与散列地址是同一类型 *//* 散列单元状态类型,分别对应:有合法元素、空单元、有已删除元素 */typedef enum {
Legitimate, Empty, Deleted } EntryType;typedef struct HashEntry Cell; /* 散列表单元类型 */struct HashEntry{
ElementType Data; /* 存放元素 */ EntryType Info; /* 单元状态 */};typedef struct TblNode *HashTable; /* 散列表类型 */struct TblNode {
/* 散列表结点定义 */ int TableSize; /* 表的最大长度 */ Cell *Cells; /* 存放散列单元数据的数组 */};HashTable BuildTable(); /* 裁判实现,细节不表 */Position Hash( ElementType Key, int TableSize ){
return (Key % TableSize);}#define ERROR -1Position Find( HashTable H, ElementType Key );int main(){
HashTable H; ElementType Key; Position P; H = BuildTable(); scanf("%d", &Key); P = Find(H, Key); if (P==ERROR) printf("ERROR: %d is not found and the table is full.\n", Key); else if (H->Cells[P].Info == Legitimate) printf("%d is at position %d.\n", Key, P); else printf("%d is not found. Position %d is returned.\n", Key, P); return 0;}/* 你的代码将被嵌在这里 */

输入样例1:(注:-1表示该位置为空。下同。)

1111 88 21 -1 -1 5 16 7 6 38 1038

输出样例1:

38 is at position 9.

输入样例2:

1111 88 21 -1 -1 5 16 7 6 38 1041

输出样例2:

41 is not found.  Position 3 is returned.

输入样例3:

1111 88 21 3 14 5 16 7 6 38 1041

输出样例3:

ERROR: 41 is not found and the table is full.

代码:

Position Find( HashTable H, ElementType Key ){
int x=Hash(Key,H->TableSize); int cnt=x; if(H->Cells[x].Data==Key||H->Cells[x].Data==-1) return x; x++; while(x
TableSize&&x!=cnt) {
if(H->Cells[x].Data==Key) return x; if(H->Cells[x].Data==-1) return x; x++; x=x%H->TableSize; } return ERROR;}

更多PTA代码请到我的博客里参考

ps:代码仅供参考,请勿抄袭

转载地址:http://tmqbi.baihongyu.com/

你可能感兴趣的文章
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
2017阿里内推笔试题--算法工程师(运筹优化)
查看>>
python自动化工具之pywinauto(零)
查看>>
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
PaperDownloader 1.5.1——更加人性化的文献下载命名解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
系统架构:Web应用架构的新趋势---前端和后端分离的一点想法
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>
自己动手写GC
查看>>
Java 8新特性终极指南
查看>>