C++实现的一个哈希表类

 

头文件:
/*----------------------------------------------------------------
// Copyright (C) 2005 一缕阳光版权所有
// 版权所有。
//
// 文件名:Hash.h
// 文件功能描述:此类简单实现了一个hash表的功能
//
// 作者:Sundy
// 创建标识:2005-06-27
//
//----------------------------------------------------------------*/
#ifndef _HASH_H_
#define _HASH_H_
#define KEYLENGTH 64  //宏定义
class CHashElem
{
 public:
    ~CHashElem(){  }
    int Key;
    int Val;
    CHashElem *pNext;
};
class CHash
{
 public:
     CHash(int iSlots = 10);
     ~CHash();
public:
    /**//*
    *功能:根据键名称,获取键值
    *@Key:键名称;
    *@Val:键值;
    *返回:If the function succeeds, the return value is true.
    */
    bool QueryValue(const int Key,int & Val);
    /**//*
    *功能:根据键名称,获取键值
    *@Key:键名称;
    *@Val:键值;
    *返回:If the function succeeds, the return value is true.
    */
     bool Insert(int Key, int Val);
     /**//*
     *功能:根据键名称,删除键
     *@Key:键名称;
     *返回:If the function succeeds, the return value is true.
     */
     bool Remove(const int Key); 
     /**//*
     *功能:获取哈希表长度
     *返回:If the function succeeds, the return value is the hash lenghth.
     */
     int GetLength(); 
     /**//*
     *功能:根据键名称,获取键值
     *返回:If the function succeeds, the return value is the hash lenghth.
     */
     int GetSlotNum();
     /**//*
     *功能:根据索引返回
     *@iIndex:键的索引号;
     *返回:If the function succeeds, the return value is CHashElem.
     */
      CHashElem*  QueryElem(const int iIndex);
 protected:
    
     CHashElem **m_pHashSlots;
     int m_iNumSlots;
     int m_iLength;
    
};
#endif
 
CPP文件:
#include "Hash.h"
CHash::CHash(int iSlots)
{
    if (iSlots < 5) iSlots = 5;
    m_pHashSlots = new CHashElem*[iSlots];
    for(int i=0;i        m_pHashSlots[i]=0;
    m_iNumSlots = iSlots;
    m_iLength=0;
}

CHash::~CHash()
{
    if (m_pHashSlots)
    {
        CHashElem *phe;
        for (int i=0;i        {
            phe = m_pHashSlots[i];
            while (phe != 0)
            {
                CHashElem *pNext = phe->pNext;
                delete phe;
                phe = pNext;
            }
        }
        delete m_pHashSlots;
        m_pHashSlots = 0;
    }
}

bool CHash::QueryValue(const int Key,int& Val)
{
    bool bRet=false;
    unsigned int num=(unsigned int)Key%m_iNumSlots;   
    if (num >= 0)
    {
        CHashElem *pElem = m_pHashSlots[num];
        while (pElem)
        {
            if (pElem->Key==Key)
            {
                Val=pElem->Val;
                bRet=true;
            }
            pElem = pElem->pNext;
        }
    }
    return bRet;
}
CHashElem*  CHash::QueryElem(const int iIndex)
{
    CHashElem *pElem=0;
    int iSlot=0;
    pElem=m_pHashSlots[0];
    for(int i=0;i<=iIndex;i++)
    {
BEGIN:
        if(iSlot        {           
            if(!pElem)
            {
                iSlot++;
                pElem=m_pHashSlots[iSlot];
                goto BEGIN;
            }
            else
            {
                pElem=pElem->pNext;
            }
        }
        else
        {
            pElem=0;
            break;
        }
    }
    return pElem;
}
bool CHash::Insert(int Key, int Val)
{
    bool bRet=false;
    unsigned int num=(unsigned int)Key%m_iNumSlots;   
    if (num >= 0)
    {
        if (m_pHashSlots[num])
        {
            CHashElem *pIns = m_pHashSlots[num];
            while (pIns->pNext)
            {
                pIns = pIns->pNext;
            }
            pIns->pNext = new CHashElem;
            pIns->pNext->pNext = 0;
            pIns->pNext->Val = Val;
            pIns->pNext->Key = Key;
            bRet=true;
            m_iLength++;
        }
        else
        {
            m_pHashSlots[num] = new CHashElem;
            m_pHashSlots[num]->pNext = 0;
            m_pHashSlots[num]->Key = Key;
            m_pHashSlots[num]->Val = Val;
            bRet=true;
            m_iLength++;
        }
    }
    return bRet;
}
 
bool CHash::Remove(const int Key)
{
    bool bRet=false;
    unsigned int num=(unsigned int)Key%m_iNumSlots;   
    if (num >= 0)
    {
        CHashElem *pElem = m_pHashSlots[num];
        CHashElem *pPrev = 0;
        while (pElem)
        {
            if (pElem->Key==Key)
            {
                if (pPrev)
                {
                    pPrev->pNext = pElem->pNext;
                }
                else
                {
                    m_pHashSlots[num] = pElem->pNext;
                }
                delete pElem;
                bRet=true;
                m_iLength--;
                break;
            }
            pPrev = pElem;
            pElem = pElem->pNext;
        }
    }
    return bRet;
}
int CHash::GetLength()
{
    return m_iLength;
}
int CHash::GetSlotNum()
{
    return m_iNumSlots;
}

 

你可能感兴趣的:(c++)