Hash(哈希)查找

#include<stdio.h>
#define MAX 10
#define key 10
void Hash_Insert(int Hash[],int n){
 int i=0,t;
 t=n%key;
 while(i<MAX){
  if(Hash[t]<=-1)
  {
   Hash[t]=n;break;
  }
  else
   t=(t+1)%key;
  i++;
 }
 if(i==MAX)
  printf("Hash is full!\n");
}

void Hash_Search(int Hash[],int x){
 int i=0,t;
 t=x%key;
 while(Hash[t]!=-1&&i<MAX){
  if(Hash[t]==x)
  {
   printf("Hash position of %d is %d\n",x,t+1);break;
  }
  else
   t=(t+1)%key;
  i++;
 }
 if(Hash[t]==-1||MAX==i)
  printf("No found!\n");
}

void main(){
 int x,i,Hash[MAX];
 for(i=0;i<MAX;i++)
  Hash[i]=-1;
 i=0;
 scanf("%d",&x);
 while(x!=-1&&i<MAX)
 {
  Hash_Insert(Hash,x);
  scanf("%d",&x);
 }
 printf("Hashlist is:");
 for(i=0;i<MAX;i++)
  printf("%d ",Hash[i]);
 printf("Input Search data:");
 scanf("%d",&x);
 Hash_Search(Hash,x);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值