欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hash表

程序员文章站 2022-03-08 16:43:28
...

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 11 //哈希表对的长度
#define key 11 // 除留取余法

void Hash__Insert(int Hash[],int x)
{
int i=0,t;
t=x%key;
while(i<MAXSIZE)
{
if(Hash[t]<=-1)
{
Hash[t]=x;
break;
}
else
t=(t+1)%key;

}

}

void Hash_Search(int Hash[],int x)
{
int i=0,t;
t=x%key;
while(Hash[t]!=-1&&i<MAXSIZE)
{
if(Hash[t]x)
{
printf(“Hash position of %d is %d\n”,x,t);
break;
}
else
t=(t+1)%key;
i++;
}
if(Hash[t]
-1||i==MAXSIZE)
printf(“no found \n”);
}
void Hash_Delete(int Hash[],int x)
{
int i=0,t;
t=x%key;
while(Hash[t]!=-1&&i<MAXSIZE)
{
if(Hash[t]==x)
{
Hash[t]=-2;
printf("%d in hashlist is deleted !\n",x);
break;
}
else
t=(t+1)%key;
i++;

}
if(i==MAXSIZE)
    printf("Delete fail\n");

}

int main()
{
int i,x,Hash[MAXSIZE];
for(i=0;i<MAXSIZE;i++)
Hash[i]=-1;
i=0;
printf(“Make Hashlist,iinput data(-1)STOP\n”);
scanf("%d",&x);
while(x!=-1&&i<MAXSIZE)
{
Hash__Insert(Hash,x);
scanf("%d",&x);
}
printf(“Output Hash\n”);
for(i=0;i<MAXSIZE;i++)
printf("%4d",Hash[i]);
printf("\n input search data\n");
scanf("%d",&x);
Hash_Search(Hash,x);
printf("\ndelete record in hashlist,input key\n");
scanf("%d",&x);
Hash_Delete(Hash,x);
printf(“output Hash list after record deleted\n”);
for(i=0;i<MAXSIZE;i++)
printf("%4d",Hash[i]);

return 0;

}

相关标签: Hash