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<iSlots;i++)
        m_pHashSlots[i]=0;
    m_iNumSlots = iSlots;
    m_iLength=0;
}

CHash::~CHash()
{
    if (m_pHashSlots)
    {
        CHashElem *phe;
        for (int i=0;i<m_iNumSlots;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<m_iNumSlots)
        {           
            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;
}

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值