建立简单的Hash table(哈希表)by C language

时间:2021-10-30 19:59:40
 #define SIZE 1000    //定义Hash table的初始大小
struct HashArray
{
int key;
int count;
struct HashArray* next;
}Hash[SIZE]; //主函数中需要初始化
void addHash(int num) //在Hash table中添加数据
{
int temp=abs(num%SIZE); //添加的数据可包括负数
if(Hash[temp].key==)
{
Hash[temp].key=num;
Hash[temp].count++;
}else if(Hash[temp].key==num)
{
Hash[temp].count++;
}else
{
struct HashArray *p=&Hash[temp];
while(p->key!=num&&p->next!=NULL)
{p=p->next;}
if(p->key==num)
{p->count++;}
else
{
p->next=(struct HashArray*)malloc(sizeof(struct HashArray));
p=p->next;
p->key=num;
p->count=;
p->next=NULL;
}
}
}