用C++实现一个哈希桶(插入,删除,寻找)

#include<iostream>
#include<vector>
#include<string>

using namespace std;
struct DefaultHashFuncString   //构造仿函数
{
	size_t operator()(const string & key)
	{
		size_t ret = 0;
		for (size_t i = 0; i < key.size(); i++)
		{
			ret += key[i];
		}
		return ret;
	}
};
template<class K>
struct DefaultHashFunc  //构造仿函数
{
	size_t operator()(const K & key)
	{
		return key;
	}
};
template<class K,class V>

struct HashTableNode  //结点结构体
{
	K _key;
	V _value;
	HashTableNode *_next;
};
template<class K, class V, class HashFunc = DefaultHashFunc<K>>
class HashTable
{
	typedef HashTableNode<K,V> Node;
public:
	HashTable(size_t size = 10)
		:_size(0)
	{
		_table.resize(size);
	}
	~HashTable()
	{
		for (size_t i = 0; i < _table.size(); i++)
		{
			Node *cur = _table[i];
			while (cur)
			{
				Node* tmp = cur;
				cur = cur->_next;
				delete tmp;
			}
		}
		_size = 0;
	}
public:
	void Insert(const K &key,const V& value) //插入
	{
		_CheckCapacity();
		size_t index = HashFun(key);
		Node *cur = _table[index];
		if (cur)
		{
			while (cur->_next)
			{
				cur = cur->_next;
			}
			cur->_next = new Node;
			cur = cur->_next;
			cur->_key = key;
			cur->_value = value;
			cur->_next = NULL;
		}
		else
		{
			_table[index] = new Node;
			_table[index]->_key = key;
			_table[index]->_value = value;
			_table[index]->_next = NULL;
		}
		++_size;
	}
	Node * Find(const K& key)  //寻找
	{
		size_t index = HashFun(key);
		Node *cur = _table[index];
		while (cur)
		{
			if (cur->_key == key)
			{
				return cur;
			}
			cur = cur->_next;
		}
		return NULL;
	}
	bool Remove(const K& key)  //删除
	{
		size_t index = HashFun(key);
		Node *cur = _table[index];
		if (!cur)
		{
			return false;
		}
		else if (cur->_next == NULL)
		{
			delete cur;
			_table[i] = NULL;
			return true;
		}
		else
		{
			Node * prev = cur;
			cur = cur->_next;
			while (cur)
			{
				if (cur->_key == key)
				{
					Node tmp = cur;
					cur = cur->_next;
					prev->next = tmp->_next;
					return true;
				}
			}
		}
	}
	void Printf()
	{
		for (size_t i = 0; i < _table.size(); i++)
		{
			printf("_table[%d]:", i);
			Node *cur = _table[i];
			while (cur)
			{
				cout << cur->_key << "->";
				cur = cur->_next;
			}
			cout << "NULL"<<endl;
		}
	}
protected:
	size_t HashFun(const K & key)  //计算index
	{
		return HashFunc()(key) % _table.size();
	}
	void _CheckCapacity()  //容量检查<span style="font-family: Arial, Helvetica, sans-serif;">(负载因子)</span>
	{
		if (_size >= _table.size())
		{
			size_t size = 2 * _size;
			HashTable<K,V>tmp;/* = new Node[size];*/
			tmp._table.resize(size);
			for (size_t i = 0; i < _table.size(); i++)
			{
				Node *cur = _table[i];
				while (cur)
				{
					tmp.Insert(cur->_key,cur->_value);
					cur = cur->_next;
				}
			}
			_table.swap(tmp._table);
		}
	}
protected:
	vector<Node*> _table;
	size_t _size;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值