温故而知新 ~ 哈希结构 ~ 程序实现一 -- 利用线性探测解决哈希冲突

该博客介绍了如何使用C++实现哈希表,特别是在解决哈希冲突时采用线性探测的闭散列方法。文中包含了哈希结构的增、删、查、改操作,并提供了具体的代码实现和运行示例。当负载因子超过0.7时,哈希表会自动扩容。此外,还展示了查找和删除元素的逻辑。
摘要由CSDN通过智能技术生成

哈希结构 ~ 程序实现一 – 利用线性探测解决哈希冲突

本篇博客将利用 C++ 中的 对哈希结构的底层进行代码实现!且在本篇程序实现中,将基于闭散列(开放定址法)利用线性探测实现对哈希冲突的解决!

代码中涉及了哈希的 inserterasefind)改(没写(~ ̄▽ ̄)~),打印等基本操作!并附带了实例以及对应的运行结果!

具体内容如下main.cpp


/* 模拟实现哈希结构! */
#include<iostream>
using namespace std;
#include<vector>

enum STATE
{
	EXIST,
	DELETE,
	EMPTY
};

//哈希表:此代码文件中哈希冲突所使用处理方法:线性探测,闭散列
template <class K,class V>
struct HashNode
{
	pair<K, V> _kv;
	STATE _st = EMPTY;
};

//顺序表实现哈希表
template <class K,class V>
class HashTable
{
public:
	typedef HashNode<K, V> Node;

	HashTable(size_t n = 10)
		:_hTable(n)
		, _size(0)
	{
		cout << _hTable.size() << endl;
	}

	// 交换哈希表中数据
	void Swap(HashTable<K, V>& newHt)
	{
		swap(_hTable, newHt._hTable);
		swap(_size, newHt._size);
	}

	//展示表中数据
	void display()
	{
		cout << "当前哈希表中数据:" << endl;
		for (int i = 0; i < _hTable.size(); i++)
		{
			cout << "Key:" << _hTable[i]._kv.first << "  Value:" << _hTable[i]._kv.second << endl;
		}
		cout << endl;
	}

	//检查容量
	void checkCapacity()
	{
		//负载因子 < 1
		//  大于0.7时进行增容
		if (_hTable.size() == 0 || _size * 10 / _hTable.size() >= (0.7 * 10))
		{
			//开新表
			int newC = _hTable.size() == 0 ? 10 : 2 * _hTable.size();
			HashTable<K, V> newHt(newC);
			for (int i = 0; i < _hTable.size(); i++)
			{
				if (_hTable[i]._st == EXIST)
					newHt.insert(_hTable[i]._kv);
			}
			Swap(newHt);
		}
	}

	bool insert(const pair<K, V>& kv)
	{
		//0、检查容量
		checkCapacity();

		//1、计算哈希位置
		int idx = kv.first % _hTable.size();

		//2、判断是否发生冲突,若有则利用线性探测!
		while (_hTable[idx]._st != EMPTY)
		{
			//若当前位置数据有效,且key相同,插入失败
			if (_hTable[idx]._st == EXIST && kv.first == _hTable[idx]._kv.first)
				return false;
			else if (_hTable[idx]._st == DELETE)
				break;
			//继续搜索
			++idx;
			if (idx == _hTable.size())
				idx = 0;
		}

		//3、插入数据
		_hTable[idx]._kv = kv;
		_hTable[idx]._st = EXIST;
		++_size;
		return true;
	}

	Node* find(const K& key)
	{
		int idx = key % _hTable.size();
		while (_hTable[idx]._st != EMPTY)
		{
			if (_hTable[idx]._st == EXIST && key == _hTable[idx]._kv.first)
				return &_hTable[idx];
			++idx;
			if (idx == _hTable.size())
				idx = 0;
		}
		return nullptr;
	}

	bool erase(const K& key)
	{
		Node* node = find(key);
		if (node)
		{
			//假删除
			--_size;
			node->_st = DELETE;
			return true;
		}
		return false;
	}

private:
	vector<Node> _hTable;
	size_t _size;
};

void test()
{
	HashTable<int, int> ht;
	ht.insert(make_pair(1, 1));
	ht.insert(make_pair(5, 5));
	ht.insert(make_pair(4, 4));
	ht.insert(make_pair(6, 6));
	ht.insert(make_pair(14, 140));
	for (int i = 15; i < 25; i++)
	{
		ht.insert(make_pair(i, i));
	}
	//ht.display();
	cout << ht.find(37) << endl;
	cout << ht.find(17)->_kv.second << endl;//显示数据
	ht.erase(17);
	cout << ht.find(17) << endl;
	ht.insert(make_pair(17, 170));
	//ht.display();
}

int main()
{
	test();
	system("pause");
	return 0;
}

运行结果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值