哈希表(开放寻址,线性探测)

#include 
#include 
#include 
#include 
using namespace std;
const int HashSize = 11;
const int NULLKEY = -1;
int m = 0;

struct HashTable
{
	int elem[HashSize];     //存储基址
	int count;              //元素数目
};
//初始化
void inithash(HashTable &H)
{
	m = HashSize;
	H.count = m;
	for(int i=0; i
//Hash函数
int Hash(int key)
{
	return key % m;
}

//插入
void InsertHash(HashTable &H, int key, int &addr)
{
	addr = Hash(key);

	while(H.elem[addr] != NULLKEY)
		addr = (addr+1) % m;
	
	H.elem[addr] = key;
}
//删除
void DeleteHash(HashTable &H, int key)
{
	int addr = Hash(key);
	if(H.elem[addr] != NULLKEY)
		addr = (addr+1)%m;
	H.elem[addr] = NULLKEY;
}

//查找
bool 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 0;
	}
	return 1;
}

//输出
void Print(const HashTable &H)
{
	for(int i=0; i vec( arr, arr + sizeof(arr)/sizeof(arr[0]) );

	HashTable H;
	inithash(H);
	cout<<"初始的Hash:"<

你可能感兴趣的:(哈希表(开放寻址,线性探测))