《大话数据结构》C++实现哈希表的创建、查找和插入

#include<iostream>

using namespace std;

typedef int status;

constexpr auto SUCCESS = 1;
constexpr auto UNSUCCESS = 0;
constexpr auto HASHSIZE = 12;
constexpr auto NULLKEY = -32768;

//哈希表结构的定义
typedef struct Hashtable
{
	int* elem;   //数据元素存储基址,动态分配数组
	int count;    //当前数据元素个数
}Hashtable;


int m = 0;  //散列表的表长,全局变量


/*相关的函数声明*/
status Inithashtable(Hashtable* H);
int Hash(int key);
void Inserthash(Hashtable* H, int key);
status Searchhash(Hashtable H, int key, int* addr);
void Createhash(Hashtable H,int array[],int n);
void Showhash(Hashtable H);


//初始化散列表(哈希表)
status Inithashtable(Hashtable* H)
{
	int i;
	m = HASHSIZE;
	H->count = m;
	H->elem = (int*)malloc(m*sizeof(int));
	for (i = 0; i < m; i++)
		H->elem[i] = NULLKEY;
	return true;
}


//散列函数
int Hash(int key)
{
	return key % m;//除留余数法
}

//插入关键字进散列表
void Inserthash(Hashtable* H, int key)
{
	int addr = Hash(key);
	while (H->elem[addr]!=NULLKEY)
	{
		addr = (addr + 1) % m;
	}
	H->elem[addr] = key;
}

//散列表查找关键字
status Searchhash(Hashtable H, int key, int* addr)
{
	*addr = Hash(key);
	while (H.elem[*addr]!=key)
	{
		*addr = (*addr + 1) % m;
		if (H.elem[*addr]==NULLKEY||*addr==Hash(key))
		{
			return UNSUCCESS;
		}
	}
	return SUCCESS;
}

//创建哈希表
void Createhash(Hashtable H, int array[], int n)
{
	for (int i = 0; i < n; i++)
	{
		Inserthash(&H,array[i]);
	}
}

//打印哈希表的值
void Showhash(Hashtable H)
{
	int num = H.count;
	for (int i = 0; i < num; i++)
	{
		cout << "第" << i << "个数字为:" << H.elem[i]<<endl;
	}
}


int main()
{
	int a[HASHSIZE] = {12,67,56,16,25,37,22,29,15,47,48,34};
	int p;
	Hashtable H;
	

	Inithashtable(&H);
	cout << "初始化后,打印的结果为:" << endl;
	Showhash(H);

	Createhash(H,a,HASHSIZE);
	cout << "初始化后,打印的结果为:" << endl;
	Showhash(H);

	
	int result = Searchhash(H,67,&p);
	cout << "67的地址是:" << p;

	return 0;
}


 

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值