哈希表

用哈希表实现CMap

#pragma once

#define NULL 0

template<typename VALUE,typename KEY,typename ARG_VALUE = const VALUE&,typename ARG_KEY = const KEY&>
class CMap
{
protected:
	struct SNode
	{
		VALUE value;
		KEY key;
		SNode *pNext;
	};
private:
	SNode * *m_pHaxi;
	int m_nCount;
public:
	CMap(const int Count = 10);
	~CMap();
	void RemoveAll();
	bool RemoveAt(ARG_KEY key);
	void SetAt(ARG_KEY key, ARG_VALUE value);
	bool LookUp(ARG_KEY key, VALUE &value);
	VALUE &operator[](ARG_KEY key);
};

template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&, 
	typename ARG_KEY = const KEY&>
CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::CMap(const int Count)
{
	m_nCount = Count;
	m_pHaxi = new SNode*[Count];
	int i = 0;
	while (i < Count)
		m_pHaxi[i++] = NULL;
}

template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&, 
	typename ARG_KEY = const KEY&>
CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::~CMap()
{
	RemoveAll();
}

template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
	typename ARG_KEY = const KEY&>
	void CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::RemoveAll()
{
	int i = 0;
	while (i < m_nCount)
	{
		SNode *p = m_pHaxi[i], *Del;
		while (p)
		{
			Del = p;
			p = p->pNext;
			delete Del;
		}
		m_pHaxi[i++] = NULL;
	}
}

	template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
		typename ARG_KEY = const KEY&>
		bool CMap<VALUE, KEY, ARG_VALUE, ARG_KEY> ::RemoveAt(ARG_KEY key)
	{
		//方法一:
		//int n = key % m_nCount;
		//SNode *p = m_pHaxi[n], *Del = p;
		要删除头结点
		//if (p->key == key)
		//{
		//	m_pHaxi[n] = p->pNext;
		//	delete p;
		//	return true;
		//}
		不存在该链表
		//else  if (!p)
		//	return false;
		//while (p)
		//{
		//	p = p->pNext;
		//	if (p->key == key)
		//	{
		//		Del->pNext = p->pNext;
		//		delete p;
		//		return true;
		//	}
		//	Del = Del->pNext;
		//}
		//return false;
		//方法二:
		int n = key % m_nCount;
		//判断头结点是否存在
		if (!m_pHaxi[n])
			return false;
		SNode* *p = &m_pHaxi[n];
		while (*p)
		{
			if ((*p)->key == key)
				break;
			p = &((*p)->pNext);
		}
		if (!*p)
			return false;
		SNode *p1 = *p;
		*p = (*p)->pNext;
		delete[]p1;
		return true;
	}

template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&, 
	typename ARG_KEY = const KEY&>
void CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::SetAt(ARG_KEY key, ARG_VALUE value)
{
	//方法一:尾插法
	//int n = key % m_nCount;
	//SNode *pNew = new SNode;
	//pNew->value = value;
	//pNew->key = key;
	//pNew->pNext = m_pHaxi[n];
	//m_pHaxi[n] = pNew;

	//头插法
	(*this)[key] = value;  //调用operator函数
}

template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
	typename ARG_KEY = const KEY&>
	bool CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::LookUp(ARG_KEY key, VALUE &value)
{
	int n = key % m_nCount;
	SNode *p = m_pHaxi[n];
	while (p)
	{
		if (p->key == key)
		{
			value = p->value;
			return true;
		}
		p = p->pNext;
	}
	return false;
}

template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
	typename ARG_KEY = const KEY&>
	VALUE &CMap<VALUE, KEY, ARG_VALUE, ARG_KEY> ::operator[](ARG_KEY key)
{
	int n = key % m_nCount;
	SNode* *p = &m_pHaxi[n];
	while (*p)
	{
		if ((*p)->key == key)
			return (*p)->value;
		p = &((*p)->pNext);
	}
	SNode *pNew = new SNode;
	pNew->key = key;
	pNew->pNext = NULL;

	*p = pNew;
	return pNew->value;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值