数据结构之---C语言实现散列表(哈希Hash表)

//散列表查找算法(Hash)
#include     
#include    
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define SUCCESS 1
#define UNSUCCESS 0
#define HASHSIZE 7 					
#define NULLKEY -32768 
typedef int Status;    
typedef struct
{
    int *elem; 						//基址
    int count; 						//当前数据元素个数 
}HashTable;

int m=0; // 散列表表长

/*初始化*/
Status Init(HashTable *hashTable)
{
    int i;
    m=HASHSIZE;
    hashTable->elem = (int *)malloc(m * sizeof(int)); //申请内存
    hashTable->count=m;
    for (i=0;ielem[i]=NULLKEY;
    }
    return OK;
}

/*哈希函数(除留余数法)*/
int Hash(int data)
{
    return data % m;
}

/*插入*/
void Insert(HashTable *hashTable,int data)
{
    int hashAddress=Hash(data); //求哈希地址

    //发生冲突
    while(hashTable->elem[hashAddress]!=NULLKEY)
    {
        //利用开放定址的线性探测法解决冲突
        hashAddress=(++hashAddress)%m;
    }

    //插入值
    hashTable->elem[hashAddress]=data;
}

/*查找*/

你可能感兴趣的:(算法,C,&&,C++,数据结构)