#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];
}
链式哈希表的实现
最新推荐文章于 2022-11-11 18:47:51 发布