哈希表之除留余数法+线性探测法,链地址法,公共溢出区法

线性探测法

#include<iostream>
using namespace std;
#define HASHSIZE 10
#define NULLKEY -32768
typedef struct hash
{
	int *element;
}HashTable;
void Init(HashTable *p)
{
	p->element = (int*)malloc(sizeof(int)*HASHSIZE);
	for (int i = 0; i < HASHSIZE; ++i)
		p->element[i] = NULLKEY;

}
// 除留余数法
int HASH(int key)
{
	return key%HASHSIZE;
}
//插入元素
void InsertHash(HashTable *p, int key)
{	
	int addr = HASH(key);
	while (p->element[addr] != NULLKEY) //处理冲突, 使用开放地址法之 线性探测法
		addr = (addr + 1) % HASHSIZE;
	p->element[addr] = key;
}
int Search(HashTable *p, int key)
{
	int addr = HASH(key);
	if (key == p->element[addr])
		return addr;
	else
	{
		addr = (addr + 1) % HASHSIZE;
		while (key != p->element[addr] && p->element[addr] != NULLKEY&&addr!=HASH(key))
			addr = (addr + 1) % HASHSIZE;
		if (key == p->element[addr])
			return addr;
	}
	return -1;
}
int main(void)
{
	int a[10] = { 12,45,2,6,78,9,0,1,15 };
	HashTable table;
	Init(&table);
	for (int i = 0; i < 10; i++)
		InsertHash(&table, a[i]);

	int res = Search(&table, 15);
	if (res!=-1)
		cout << "查找成功!地址为: "<<res<<endl;
	else
		cout << "查找失败! "<<endl;
	system("pause");
	return 0;
}

链地址法

#include<iostream>
using namespace std;
#define HASHSIZE 10
typedef struct node
{
	int num;
	struct node *next;
}NODE;
typedef struct hash
{
	NODE *element[HASHSIZE];
	int count;
}HashTable;
void Init(HashTable *p)
{	
	for (int i = 0; i < HASHSIZE; ++i)
	{
		p->element[i] = (NODE*)malloc(sizeof(NODE)*HASHSIZE);
		p->element[i]->next = NULL;
	}

}
// 除留余数法
int HASH(int key)
{
	return key%HASHSIZE;
}
//插入元素
void InsertHash(HashTable *p, int key)
{	
	int addr = HASH(key);
	//采用链地址法。  头插法
	NODE *q = (NODE*)malloc(sizeof(NODE));
	q->num = key;
	q->next = p->element[addr]->next;
	p->element[addr]->next = q;
	
}
int Search(HashTable *p, int key)
{
	int addr = HASH(key);
	NODE *m = p->element[addr]->next;
	while (m != NULL)
	{
		if (m->num == key)
			return addr;
		m = m->next;
	}
	return -1;
}
int main(void)
{
	int a[10] = { 12,45,2,6,78,9,0,1,15 };
	HashTable table;
	Init(&table);
	for (int i = 0; i < 10; i++)
		InsertHash(&table, a[i]);

	int res = Search(&table, 12);
	if (res!=-1)
		cout << "查找成功!地址为: "<<res<<endl;
	else
		cout << "查找失败! "<<endl;
	system("pause");
	return 0;
}
公共溢出区法

#include<iostream>
using namespace std;
#define HASHSIZE 10
#define NULLKEY -32768
typedef struct hash
{
	int *element;
	int *extraElement;//溢出区表
	int count; // 溢出区的元素个数
}HashTable;
// 由于 通过哈希函数 计算后的地址重复几率很小,所以把重复部分用顺序表来存放,查找时遍历这个公共溢出区的顺序表,效率会更高。
void Init(HashTable *p)
{
	p->element = (int*)malloc(sizeof(int)*HASHSIZE);
	p->extraElement = (int*)malloc(sizeof(int)*HASHSIZE);
	for (int i = 0; i < HASHSIZE; ++i)
	{
		p->element[i] = NULLKEY;
		p->extraElement[i] = NULLKEY; 
	}
	p->count = 0; 
}
// 除留余数法
int HASH(int key)
{
	return key%HASHSIZE;
}
//插入元素
void InsertHash(HashTable *p, int key)
{
	int addr = HASH(key);
	//处理冲突, 使用公共溢出法
	if (p->element[addr] == NULLKEY)
		p->element[addr] = key;
	else //如果元素内已经有值了,就放到溢出部分的表中,且把count加 1
		p->extraElement[p->count++] = key;
}
int Search(HashTable *p, int key)
{
	int addr = HASH(key);
	if (key == p->element[addr])
		return addr;
	else
	{
		for (int i = 0; i < p->count; ++i)
			if (p->extraElement[i] == key)
			{
				cout << "溢出表" << endl;
				return i;
			}
	}
	return -1;
}
int main(void)
{
	int a[10] = { 12,45,2,6,78,9,0,1,15 };
	HashTable table;
	Init(&table);
	for (int i = 0; i < 10; i++)
		InsertHash(&table, a[i]);

	int res = Search(&table, 2);
	if (res != -1)
		cout << "查找成功!地址为: " << res << endl;
	else
		cout << "查找失败! " << endl;
	system("pause");
	return 0;
}



  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值