7-1 实验5-1(散列)
利用开放定址法散列表的创建、插入、删除和查找操作,使用线性探测法处理冲突。散列表长度TableSize设置为11,散列函数定义为h(key) = key % TableSize。
输入格式:
输入第一行是一个整数n,表示后面有n行输入。
每行输入表示对散列表的一条操作指令,格式是“指令编号 参数”。
指令编号为1,表示插入操作,此时参数为插入散列表的key,无输出。
指令编号为2,表示删除操作,此时参数为待删除的key,无输出。
指令编号为3,表示查找操作,此时参数为待查找的key,输出查找这个key所需进行的比较次数,之后输出换行符。
例如:
6
1 8
1 19
3 19
2 8
1 30
3 30
输出格式:
进行插入和删除操作时无输出。进行查找操作时,输出查找key所需进行的比较次数及换行符。
例如,对于上面的示例输入,输出为:
2
1
说明:第1行指令为插入8,第2行指令为插入19(与8冲突,因此存入8之后的空位)。第3行指令为查找19,需要比较2次(第1次与8比较,第2次与19比较),因此输出2。第4行指令为删除8(懒惰删除),第5行指令为插入30(插在原先8的存储位置),第6行指令为查找30,只需进行1次比较,因此输出1。
输入样例:
在这里给出一组输入。例如:
10
1 5
1 6
1 16
1 7
3 16
2 5
3 5
1 27
3 27
3 5
输出样例:
在这里给出相应的输出。例如:
3
1
1
5
代码长度限制 16 KB
时间限制 400 ms
内存限制 64 MB
栈限制 8192 KB
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXTABLESIZE 100000 // 允许开辟的最大散列列表长度
typedef int ElementType; // 关键词类型,使用整型
typedef int Index; // 散列地址类型
typedef Index Position; // 数据所在位置与散列地址是同一类型
// 散列单元状态类型,分别对应:有合法元素、空单元、有已删除元素
typedef enum {Legitimate, Empty, Delete} EntryType;
typedef struct HashEntry Cell; // 散列表单元类型
struct HashEntry {
ElementType Data; // 存放元素
EntryType Info; // 单元状态
};
typedef struct TblNode * HashTable; // 散列表类型
struct TblNode {
int TableSize; // 散列表的最大长度
Cell* Cells; // 存放散列单元的指针
};
HashTable CreatTable(int TableSize) {
// 创建散列表
HashTable H = (HashTable)malloc(sizeof(struct TblNode));
H->TableSize = TableSize; // 保证散列表最大长度是素数
H->Cells = (Cell *)malloc(H->TableSize * sizeof(Cell)); // 声明单元数组
// 初始化单元格状态为空单元
for(int i = 0; i < H->TableSize; i++)
H->Cells[i].Info = Empty;
return H;
}
Position Hash(ElementType key, int TableSize) {
// 哈希函数
return key % TableSize;
}
Position Find_Linear(HashTable H, ElementType key) {
// 线性探测法查找元素Key并返回其位置Pos,如果找不到则Pos信息为Empty或者Delete,不是Legitimaye
int Pos = Hash(key, H->TableSize); // 初始查找位置
// 查找操作
while (true) {
if (H->Cells[Pos].Info != Legitimate) { // 如果遇到空单元,则查找失败
break;
}
// 如果不是空单元
if (H->Cells[Pos].Info == Legitimate && H->Cells[Pos].Data == key) {
// 找到Key
break;
}
// 当前单元格不是Key,试探下一个单元格
Pos = (Pos + 1) % H->TableSize;
}
return Pos; // 查找成功,返回Pos,查找失败,则返回空单元位置
}
Position Count_Find_Linear(HashTable H, ElementType key) {
// 线性探测法查找元素Key并返回其位置Pos,如果找不到则Pos信息为Empty或者Delete,不是Legitimaye
int Pos = Hash(key, H->TableSize); // 初始查找位置
int count = 0;
// 查找操作
while (true) {
count ++;
if (H->Cells[Pos].Info != Legitimate) { // 如果遇到空单元,则查找失败
break;
}
// 如果不是空单元
if (H->Cells[Pos].Info == Legitimate && H->Cells[Pos].Data == key) {
// 找到Key
break;
}
// 当前单元格不是Key,试探下一个单元格
Pos = (Pos + 1) % H->TableSize;
}
return count; // 返回查找次数
}
bool Insert_Linear(HashTable H, ElementType Key) {
// 线性探测法插入函数
Position Pos = Find_Linear(H, Key); // 先查找Key是否存在,存在返回Pos,不存在则返回Empty或delete的Pos
// 如果key不存在
if(H->Cells[Pos].Info != Legitimate) {
// 如果这个单元格没有被占用,说明Key可以插入在此,会覆盖Delete
H->Cells[Pos].Info = Legitimate;
H->Cells[Pos].Data = Key;
return true;
}else {
printf("键值已存在"); // 如果存在key,不插入
return false;
}
}
bool Detete_Linear(HashTable H, ElementType Key) {
// 线性探测法删除操作
Position Pos = Find_Linear(H, Key);
if(H->Cells[Pos].Info == Legitimate) {
H->Cells[Pos].Info = Delete; //标记为删除
return true;
}
return false;
}
int main(int argc, char *argv[]) {
int n, command, key;
HashTable H = CreatTable(11);
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d %d", &command, &key);
switch (command) {
case 1: // 插入
Insert_Linear(H, key);
break;
case 2: // 删除
Detete_Linear(H, key);
break;
case 3: // 查找
printf("%d\n", Count_Find_Linear(H, key));
break;
}
}
// 释放内存
free(H->Cells);
free(H);
return 0;
}

被折叠的 条评论
为什么被折叠?



