hash表简单实现

hash就是以空间换时间。如果有冲突就以链表插入 来解决:

#include <iostream>
#include <vector>
using namespace std;

class MyHash
{
public:
	class  hash_node
	{
	public:
		hash_node():value(0)
			,next(nullptr)
		{

		}
		int value;
		hash_node* next;
	};
	MyHash() : m_TableSize(127)
	{
		m_pHashTable = new hash_node* [m_TableSize];
		for (int i = 0; i < m_TableSize; ++i)
		{
			m_pHashTable[i] = nullptr;
		}
	}
	~MyHash()
	{
		delete []m_pHashTable;
	}

	void Insert(int aValue)
	{
		const int index = GetHashValue(aValue);
		hash_node* lpNode;
		bool lRes = Search(index,lpNode);
		if (!lRes)
		{
			if (lpNode == nullptr)
			{
				hash_node* lpNewNode = new hash_node;
				lpNewNode->value = aValue;
				lpNode->next = lpNewNode;
			}
			else
			{
				hash_node* lpNewNode = new hash_node;
				lpNewNode->value = aValue;
				lpNewNode->next = nullptr;
				m_pHashTable[index] = lpNewNode;
			}
		}
	}

	bool Search(int aValue, hash_node*& apNode)
	{
		const int index = GetHashValue(aValue);
		auto lpNode = m_pHashTable[index];
		if (nullptr == lpNode)
		{
			return false;
		}
		if (lpNode->value == aValue)
		{
			return true;
		}

		hash_node* lpPreNode = lpNode;
		hash_node* lpNextNode = lpPreNode->next;
		while (lpNextNode)
		{
			if (lpNextNode->value == aValue)
			{
				return true;
			}
			lpPreNode = lpNextNode;
			lpNode = lpPreNode->next;
		}
		apNode = lpPreNode;
		return true;
	}

	void Find(int value)
	{
		hash_node* lpNode;
		if (Search(value, lpNode))
		{
			std::cout << "Find out " << value << std::endl;
		}
		else
		{
			std::cout << "Not Find.\n";
		}
	}
private:
	int GetHashValue(int aValue)
	{
		return aValue%m_TableSize;
	}

	int m_TableSize;
	hash_node** m_pHashTable;
};

int main()
{
	MyHash lHash;
	int lArray[] = {1,45,24,67,33,23,172,151};
	int lSize = sizeof(lArray)/sizeof(int);
	for (int i = 0; i < lSize; ++i)
	{
		lHash.Insert(lArray[i]);
	}

	for (int i = 0; i < lSize; ++i)
	{
		lHash.Find(lArray[i]);
	}
	
	return 0;
}



只是最简单的形式,把key的value混用了,现在把它们分开搞:

#include <iostream>
#include <vector>
using namespace std;

class MyHash
{
public:
	class  hash_node
	{
	public:
		hash_node():key(0)
			, value(0)
			,next(nullptr)
		{

		}
		int key;
		int value;
		hash_node* next;
	};
	MyHash() : m_TableSize(127)
	{
		m_pHashTable = new hash_node* [m_TableSize];
		for (int i = 0; i < m_TableSize; ++i)
		{
			m_pHashTable[i] = nullptr;
		}
	}
	~MyHash()
	{
		delete []m_pHashTable;
	}

	void Insert(int aKey, int aValue)
	{
		const int index = GetHashValue(aKey);
		hash_node* lpNode;
		bool lRes = Search(index,aValue,lpNode);
		if (!lRes)
		{
			if (lpNode == nullptr)
			{
				hash_node* lpNewNode = new hash_node;
				lpNewNode->key = aKey;
				lpNewNode->value = aValue;
				lpNode->next = lpNewNode;
			}
			else
			{
				hash_node* lpNewNode = new hash_node;
				lpNewNode->key = aKey;
				lpNewNode->value = aValue;
				lpNewNode->next = nullptr;
				m_pHashTable[index] = lpNewNode;
			}
		}
	}

	bool Search(int aKey,int aValue, hash_node*& apNode)
	{
		const int index = GetHashValue(aKey);
		auto lpNode = m_pHashTable[index];
		if (nullptr == lpNode)
		{
			return false;
		}
		if (lpNode->value == aValue)
		{
			return true;
		}

		hash_node* lpPreNode = lpNode;
		hash_node* lpNextNode = lpPreNode->next;
		while (lpNextNode)
		{
			if (lpNextNode->value == aValue)
			{
				return true;
			}
			lpPreNode = lpNextNode;
			lpNode = lpPreNode->next;
		}
		apNode = lpPreNode;
		return true;
	}

	void Find(int aKey, int aValue)
	{
		hash_node* lpNode;
		if (Search(aKey, aValue, lpNode))
		{
			std::cout << "Find out " << aValue << std::endl;
		}
		else
		{
			std::cout << "Not Find.\n";
		}
	}
private:
	int GetHashValue(int aValue)
	{
		return aValue%m_TableSize;
	}

	int m_TableSize;
	hash_node** m_pHashTable;
};

int main()
{
	MyHash lHash;
	int lArray[] = {1,45,24,67,33,23,172,151};
	int lSize = sizeof(lArray)/sizeof(int);
	for (int i = 0; i < lSize; ++i)
	{
		lHash.Insert(lArray[i],lArray[i]);
	}

	for (int i = 0; i < lSize; ++i)
	{
		lHash.Find(lArray[i],lArray[i]);
	}
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值