练习五: 散列(线性探查)

【基本要求】
编写一个程序,实现哈希表的初始化、插入、查找、删除等相关运算,并在此基础上完成如下功能:
(1) 建立{16,74,60,43,54,90,46,31,29,88,77}哈希表A[0…12],哈希函数为H(k)=key%p,(p取13),并采用线性探查法解决冲突。
(2) 在上述哈希表中查找关键字为29的记录。
(3) 在上述哈希表中删除关键字为77的记录,再将其插入。
【输出结果】
输出结果例子如下:
哈希表地址: 0 1 2 3 4 5 6 7 8 9 10 11 12
哈希表关键字: 77 54 16 43 31 29 46 60 74 88 90
搜索次数: 2 1 1 1 1 4 1 1 1 1 1
平均搜索长度ASL(11)=1.36364
ha[6].key=29
删除关键字77
哈希表地址: 0 1 2 3 4 5 6 7 8 9 10 11 12
哈希表关键字: 54 16 43 31 29 46 60 74 88 90
搜索次数: 1 1 1 1 4 1 1 1 1 1
平均搜索长度ASL(10)=1.3
未找到77
插入关键字77
哈希表地址: 0 1 2 3 4 5 6 7 8 9 10 11 12
哈希表关键字: 77 54 16 43 31 29 46 60 74 88 90
搜索次数: 2 1 1 1 1 4 1 1 1 1 1
平均搜索长度ASL(11)=1.36364

#include <stdio.h>
#include <stdlib.h>

enum State
{
	EXIST,
	EMPTY,
	DELETE
};

typedef struct HashCell
{
	int Key;
	int Searchtime;
	State Info;
};

typedef struct HashTable
{
	int TableSize;
	HashCell *TheCells;
}*Hash;

void InitHash(Hash H)
{
	H->TheCells = (HashCell*)malloc(sizeof(HashCell) * H->TableSize);
	for (int i = 0; i < H->TableSize; i++)
		H->TheCells[i].Info = EMPTY;
}

int InsertHash(Hash H, int Key)
{
	int Index, i, j = 0, searchtime = 0;
	Index = Key % H->TableSize;
	for (i = Index; i < H->TableSize; i++)
	{
		j++;
		searchtime++;
		if (H->TheCells[i].Info != EXIST)
			break;
		if (i == H->TableSize - 1)
			i = -1;
		if (j == H->TableSize)
			return -1;
	}
	H->TheCells[i].Info = EXIST;
	H->TheCells[i].Key = Key;
	H->TheCells[i].Searchtime = searchtime;
	return 0;
}

int FindHash(Hash H, int Key)
{
	int Index, i, j = 0;
	Index = Key % H->TableSize;
	for (i = Index; i < H->TableSize; i++)
	{
		j++;
		if (H->TheCells[i].Key == Key && H->TheCells[i].Info == EXIST)
			return i;
		if (i == H->TableSize - 1)
			i = -1;
		if (j == H->TableSize)
			return -1;
	}
	return 0;
}

int DeleteHash(Hash H, int Key)
{
	int Index = FindHash(H, Key);
	if (Index == 0)
		H->TheCells[Index].Info = DELETE;
	else
		return -1;
	return 0;
}

double ASL(Hash H)
{
	double j = 0, asl = 0;
	for (int i = 0; i < H->TableSize; i++)
	{
		if (H->TheCells[i].Info != EXIST)
			asl += 0;
		else
		{
			j++;
			asl += H->TheCells[i].Searchtime;
		}
	}
	asl /= j;
	return asl;
}

void PrintHash(Hash H)
{
	int i;
	printf("哈希表地址:       ");
	for (i = 0; i < H->TableSize; i++)
		printf("%d  ", i);
	printf("\n哈希表关键字:     ");
	for (i = 0; i < H->TableSize; i++)
	{
		if (H->TheCells[i].Info != EXIST)
			printf("   ");
		else
			printf("%d ", H->TheCells[i].Key);
	}
	printf("\n搜索次数:          ");
	for (i = 0; i < H->TableSize; i++)
	{
		if (H->TheCells[i].Info != EXIST)
			printf("   ");
		else
			printf("%d  ", H->TheCells[i].Searchtime);
	}
	printf("\n平均搜索长度ASL(11)= %lf\n", ASL(H));
}

int main()
{
	Hash H;
	H = (Hash)malloc(sizeof(HashTable));
	H->TableSize = 13;
	InitHash(H);	
	int data[] = { 16,74,60,43,54,90,46,31,29,88,77 };
	for (int i = 0; i < 11; i++)
		InsertHash(H, data[i]);
	PrintHash(H);
	printf("ha[%d].key=29\n", FindHash(H,29));
	printf("删除关键字77\n");
	(DeleteHash(H, 77) == 0) ? PrintHash(H) : printf("删除失败!\n");
	(FindHash(H, 77) == 0) ? PrintHash(H) : printf("未找到77!\n");
	(InsertHash(H,77) == 0) ? PrintHash(H) : printf("插入失败!!\n");
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值