Hash表
#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;
}
上一篇: Django和restfull
下一篇: java多线程-分析一段代码的输出结果