Hash Table

来源:互联网 发布:php微信群抢红包接口 编辑:程序博客网 时间:2024/04/30 11:46

#ifndef _HASH_H_
#define _HASH_H_

#include <stdlib.h>
#include <stdio.h>

//Hash element define

typedef struct HASH_ELEM
{
       int iKey;
       int iData; 
       struct HASH_ELEM *pNext;

}HASH_ELEM;

//hash table Head

typedef struct HASH
{
       HASH_ELEM ** pHashSlot;
       unsigned int iTotalSlots;
       int iElemCount;
}HASH;


//
HASH *HashCreate(int iHashSlots);
unsigned int GetHashSlot(int iKey, HASH *pHash);
int HashInsert(HASH_ELEM *pHashElem, HASH *pHash);
void HashDestroy(HASH *pHash);
HASH_ELEM * GetHashElem(int iKey, HASH *pHash);
 int DeleteHashElem(int iKey, HASH *pHash);

#endif
/*********************************hash.h**************************************

#include "hash.h"

#define _DEBUG_MAIN_

//创建hashtable 返回Hash * error return NULL

HASH *HashCreate(int iTotalSlots)
{
 int i;
 
 HASH * pHash = (HASH*)malloc(sizeof(HASH));
 if (pHash == NULL)
 {
  printf("hash malloc error in hashcreate()/n");
  return NULL;

 }
 pHash->pHashSlot = (HASH_ELEM**)malloc(sizeof(HASH_ELEM*) *iTotalSlots);
 if (pHash->pHashSlot == NULL)
 {
  printf("hash->pHashSlot malloc error. freeing hash./n");
  free(pHash);
  return NULL;
 }
 pHash->iTotalSlots = iTotalSlots;
 for(i=0;i<iTotalSlots;i++)
 {
  pHash->pHashSlot[i] = NULL;
 }
 pHash->iElemCount = 0;
 return pHash;
}

//locate the  hash slot bye hashkey

unsigned int GetHashSlot(int iKey, int iTotalSlot)
{
 unsigned int iSlot;
 iSlot = iKey%iTotalSlot;

 return iSlot;

}

//insert a hash element into the hashtable
int HashInsert(HASH_ELEM *pHashElem, HASH *pHash)
{
 int iSlot;
 iSlot = GetHashSlot(pHashElem->iKey, pHash->iTotalSlots);

 if (pHash->pHashSlot[iSlot] == NULL)
 {
  pHash->pHashSlot[iSlot] = (HASH_ELEM*)malloc(sizeof(HASH_ELEM));
  (pHash->pHashSlot[iSlot])->iData = pHashElem->iData;
  (pHash->pHashSlot[iSlot])->iKey= pHashElem->iKey;
  (pHash->pHashSlot[iSlot])->pNext= NULL;

  pHash->iElemCount++; 
 }
 else
 {
  HASH_ELEM * pInsert = pHash->pHashSlot[iSlot];
  while(pInsert->pNext)
  {
   pInsert = pInsert->pNext;
  }
  pInsert->pNext= (HASH_ELEM *)malloc(sizeof(HASH_ELEM));
  pInsert->pNext->iData = pHashElem->iData;
  pInsert->pNext->iKey = pHashElem->iKey;
  pInsert->pNext->pNext = NULL;

  pHash->iElemCount++;
 }
 return pHash->iElemCount;

}

//free hashtable
void HashDestroy(HASH *pHash)
{
 int i;
 
 if (pHash ==NULL)
 {
  return;
 }

 HASH_ELEM *pDelete;
 for(i=0;i<pHash->iTotalSlots;i++)
 {
  pDelete = pHash->pHashSlot[i];
  while(pDelete)
  {
   HASH_ELEM * pNext = pDelete->pNext;
   free(pDelete);
   pDelete = pNext;
  }
 }
 free(pHash->pHashSlot);
 free(pHash);
}


//get hashtable element suc:return the point err:return NULL
HASH_ELEM * GetHashElem(int iKey, HASH *pHash)
{
 int iSlot;
 iSlot = GetHashSlot(iKey, pHash->iTotalSlots);
 if (pHash->pHashSlot[iSlot] == NULL)
 {
  return NULL;
 }
 HASH_ELEM *pHashElem = pHash->pHashSlot[iSlot];
 while(pHashElem)
 {
  if (pHashElem->iKey == iKey)
  {
   return pHashElem;
  }
  pHashElem = pHashElem->pNext;
 }
 return NULL;

}

//delete hashtable elem by hashkey
 int DeleteHashElem(int iKey, HASH *pHash)
 {
 int iSlot;
 iSlot = GetHashSlot(iKey, pHash->iTotalSlots);
 if (pHash->pHashSlot[iSlot] == NULL)
 {
  return 0;
 }
 HASH_ELEM *pHashElem = pHash->pHashSlot[iSlot];
 HASH_ELEM *pPreElem = NULL;
 while(pHashElem)
 {
  if (pHashElem->iKey == iKey)
  {
   if (pPreElem)
   {
    pPreElem->pNext = pHashElem->pNext;
    free(pHashElem);
    pHash->iElemCount--;
    return 1;
   }
   else
   {
    pHash->pHashSlot[iSlot] = NULL;
    free(pHashElem);
    pHash->iElemCount--;
    return 1;
   }
  }
  pPreElem = pHashElem;
  pHashElem = pHashElem->pNext;
 }

 return 0;

 }

 #ifdef _DEBUG_MAIN_
 int main()
 {
  int i;
  int iTotalSlot = 5;
 HASH * pHash = HashCreate(iTotalSlot);
 HASH_ELEM hashelem;

 for(i=0;i<17;i++)
 {
  bzero(&hashelem, sizeof(hashelem));
  hashelem.iData = i;
  hashelem.iKey = i;
  HashInsert(&hashelem, pHash);
 }
 printf("after insert total %d elem in hashtable/n", pHash->iElemCount);

 HASH_ELEM * pElem = GetHashElem(14, pHash);
 printf("hashelem[key:%d,value:%d]/n",pElem->iKey,pElem->iData);

 int iRet = DeleteHashElem(14, pHash);
 printf("Delete HashElem 14 iRet =%d remain %d elems/n",iRet, pHash->iElemCount);
 iRet = DeleteHashElem(18, pHash);
 printf("Delete HashElem 18 iRet =%d remain %d elems/n",iRet, pHash->iElemCount);


 HashDestroy(pHash);
 }

#endif
/***********************hash.c****************************************

原创粉丝点击