C++实现基于数组+链表的简易版HashMap

1.HashMap原理示意图

2.C++代码实现

#include <iostream>
using namespace std;

typedef struct HashNode
{
	int val;
	struct HashNode* next;
}HashNode;

class hash_map 
{
public:
	hash_map();
	hash_map(const int&_key);
	void insert(const int& _val);              /*插入*/
	bool find(const int& _val);                /*查找*/
	bool del_value(const int& _val);           /*删除值*/
	bool empty();                              /* 是否为空表*/
	int size();                                /*返回插入元素的数量*/
	void clear();                              /* 清空表*/
	void print();                              /*打印表*/
	~hash_map();

private:
	HashNode* buffer;
	int _size;
	int key;

};

hash_map::hash_map()
{
	key = 13;
	_size = 0;
	buffer = new HashNode[key];
	for (int i = 0; i < key; i++)
	{
		buffer[i].next = nullptr;
	}
}

hash_map::hash_map(const int& _key) : key(_key), _size(0)
{
	buffer = new HashNode[key];
	for (int i = 0; i < key; i++)
	{
		buffer[i].next = nullptr;
	}
}

void hash_map::insert(const int& _val)
{
	HashNode* newnode = new HashNode;
	newnode->val = _val;
	newnode->next = nullptr;
	HashNode *cur = buffer[newnode->val % key].next;
	buffer[newnode->val % key].next = newnode;
	newnode->next = cur;
	_size++;
}

bool hash_map::find(const int& _val)
{
	int idx = _val % key;
	HashNode* cur = buffer[idx].next;
	while (cur)
	{
		if (cur->val == _val)
			return true;
		cur = cur->next;
	}
	return false;
}

bool hash_map::del_value(const int& _val)
{
	int idx = _val % key;
	HashNode* cur = &buffer[idx];
	while (cur)
	{
		if (cur->next && cur->next->val == _val)
		{
			HashNode* t = cur->next;
			cur->next = t->next;
			delete t;
			_size--;
		}
		else
			cur = cur->next;
	}
	return true;
}

bool hash_map::empty()
{
	return _size == 0;
}

int hash_map::size()
{
	return _size;
}

void hash_map::clear()
{
	for (int i = 0; i < key; i++)
	{
		HashNode* cur = buffer[i].next;
		while (cur)
		{
			HashNode* t = cur->next;
			buffer[i].next = t;
			delete cur;
			cur = t;
		}
	}
	_size = 0;
}

void hash_map::print()
{
	for (int i = 0; i < key; i++)
	{
		cout << "[" << i << "]:->";
		HashNode* cur = buffer[i].next;
		while (cur)
		{
			cout << cur->val;
			cur = cur->next;
			if (cur)
				cout << "->";
		}
		cout << endl;
	}
}

hash_map::~hash_map()
{
	clear();
	delete[]buffer;
}



int main()
{
	hash_map mymap(13);
	for (int i = 0; i < 39; i++)
		mymap.insert(i);
	mymap.print();
	cout << "size = " << mymap.size() << endl;
	cout << "find(10):"<<mymap.find(10) << endl;
	cout << "find(40):" << mymap.find(40) << endl;
	cout << "=====================" << endl;
	mymap.del_value(10);
	mymap.print();
	cout << "find(10):" << mymap.find(10) << endl;
	cout << "find(40):" << mymap.find(40) << endl;
	cout << "size = " << mymap.size() << endl;
	return 0;
}

3.运行结果

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你看,那是海边

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值