链式哈希表的实现

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) {if(p) {delete p;p=nullptr;}}
#endif

#ifndef SAFE_DELARR
#define SAFE_DELARR(p) {if(p) {delete []p;p=nullptr;}}
#endif

template<class H,size_t TableSize>
class HashTable_StringKey
{
public:
struct Node
{
  char *pKey;
  H HashData;
  Node *pFront;
  Node *pNext;
};
struct HashTable
{
 Node *m_pHead;
 Node *m_pEnd;
};
HashTable_StringKey()
{
  for(int i=0;i<TableSize;++i)
  {
     m_HashTableData[i].m_pHead=nullptr;
     m_HashTableData[i].m_pEnd=nullptr;
  }
}

int HashFunction(char const * const key)//time33方法,把key映射到表里的某个位置,尽可能分散
{
  int keyindex=0;
  int hashindex=0;
  while(key[keyindex])//key[abc]=97+(97*32+98)+((97*32+98)*32+99)
  {
    hashindex=(hashindex<<5)+key[keyindex++];
  }
  return hashindex%TableSize;
}

Node *Find(char const * const key)
{
  int hashindex=HashFunction(key);
  if(m_HashTableData[hashindex].m_pHead)
  {
    Node *pCurrentNode=m_HashTableData[hashindex].m_pHead;
    while(pCurrentNode)
    {
      	if (strcmp(pCurrentNode->pKey,key)==0)//字符串比较
		{
		   return pCurrentNode;
		}
      pCurrentNode=pCurrentNode->pNext;
    }
  }
  return false;
}

void Insert(char const *const key, H const &hashData)
{
	Node *pNode = Find(key);
	if (pNode)
	{
		pNode->HashData = hashData;
	}
	else
	{
		Node *pNewNode = new Node;
		pNewNode->pKey = new char[strlen(key) + 1];
		strcpy_s(pNewNode->pKey, strlen(key) + 1, key);
		pNewNode->HashData = hashData;
		pNewNode->pFront = nullptr;
		pNewNode->pNext = nullptr;

		int hashindex = HashFunction(key);
		if (m_HashTableData[hashindex].m_pHead) //头插
		{
			pNewNode->pNext = m_HashTableData[hashindex].m_pHead;
			pNewNode->pFront = nullptr;
			m_HashTableData[hashindex].m_pHead->pFront = pNewNode;
			m_HashTableData[hashindex].m_pHead = pNewNode;
		}
		else
		{
			m_HashTableData[hashindex].m_pHead = pNewNode;
			m_HashTableData[hashindex].m_pEnd = pNewNode;
		}
	}
}
H& operator[](char const * const key)
{
	Node *pNode = Find(key);
	if (pNode)
	{
		return pNode->HashData;
	}
	else
	{
		Node *pNewNode = new Node;
		pNewNode->pKey = new char[strlen(key) + 1];
		strcpy_s(pNewNode->pKey, strlen(key) + 1, key);
		pNewNode->HashData = H();
		pNewNode->pFront = nullptr;
		pNewNode->pNext = nullptr;

		int hashindex = HashFunction(key);
		if (m_HashTableData[hashindex].m_pHead)
		{
			pNewNode->pNext = m_HashTableData[hashindex].m_pHead;
			pNewNode->pFront = nullptr;
			m_HashTableData[hashindex].m_pHead->pFront = pNewNode;
			m_HashTableData[hashindex].m_pHead = pNewNode;
		}
		else
		{
			m_HashTableData[hashindex].m_pHead = pNewNode;
			m_HashTableData[hashindex].m_pEnd = pNewNode;

		}
		return pNewNode->HashData;
	}
}
bool Delete(char const * const key)
{
	Node *pNode = Find(key);
	if (pNode)
	{
		int hashindex = HashFunction(key);
		Node *pDeleteNode = nullptr;
		if (pNode==m_HashTableData[hashindex].m_pHead)//头删
		{
		pDeleteNode = m_HashTableData[hashindex].m_pHead;
		m_HashTableData[hashindex].m_pHead = m_HashTableData[hashindex].m_pHead->pNext;
			if (m_HashTableData[hashindex].m_pHead)
			{
				m_HashTableData[hashindex].m_pHead->pFront = nullptr;
			}
			else
			{
				m_HashTableData[hashindex].m_pEnd= nullptr;
			}
				
			SAFE_DELARR(pDeleteNode->pKey);
			SAFE_DELETE(pDeleteNode);
		}
		else
		{
			if (pNode == m_HashTableData[hashindex].m_pEnd)//尾删
			{
		pDeleteNode = m_HashTableData[hashindex].m_pEnd;
		m_HashTableData[hashindex].m_pEnd = m_HashTableData[hashindex].m_pEnd->pFront;
		m_HashTableData[hashindex].m_pEnd->pNext = nullptr;
		SAFE_DELARR(pDeleteNode->pKey);
	    SAFE_DELETE(pDeleteNode);
			}
			else//中间删
			{
					pNode->pFront->pNext = pNode->pNext;
					pNode->pNext->pFront = pNode->pFront;
					SAFE_DELARR(pDeleteNode->pKey);
					SAFE_DELETE(pNode);
			}
		}
		return true;
	}
	return false;
}

~HashTable_StringKey()
{
  for(int i=0;i<TableSize;++i)
  { 
     Node *pDeleteNode=nullptr;  
     while(m_HashTableData[i].m_pHead)
     {
         pDeleteNode=m_HashTableData[i].m_pHead;
         m_HashTableData[i].m_pHead=m_HashTableData[i].m_pHead->pNext;
         if(m_HashTableData[i].m_pHead)
         {
            m_HashTableData[i].m_pHead->pFront=nullptr;
         }
         else
         {
            m_HashTableData[i].m_pEnd = nullptr;
         }
        SAFE_DELARR(pDeteleNode->pKey);
		SAFE_DELETE(pDeteleNode);
     }
  }
}
protected:
  HashTable m_HashTableData[TableSize];

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值