#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
int flag;
};
struct hash{
int size;
struct node *cell;
};
typedef struct hash *HashTable;
HashTable initial(int P)
{
HashTable H=malloc(sizeof(struct hash));
H->size=P;
H->cell=malloc(sizeof(struct node)*H->size);
while(P)
{
H->cell[--P].flag=-1;
}
return H;
}
int Hash(int key,int P)
{
return key%P;
}
int Find(int key,HashTable H)
{
int pos=Hash(key,H->size);
while((H->cell[pos].flag!=-1)&&(H->cell[pos].data!=key))
{
pos++;
if(pos==H->size)
pos=pos-H->size;
}
return pos;
}
void InsertAndOutput(int key,HashTable H)
{
int pos=Find(key,H);
if(H->cell[pos].flag==-1)
{
H->cell[pos].flag=0;
H->cell[pos].data=key;
}
printf("%d",pos);
}
main()
{
int N,P,i,key;
HashTable H;
scanf("%d %d",&N,&P);
H=initial(P);
scanf("%d",&key);
InsertAndOutput(key,H);
for(i=1;i<N;i++)
{
scanf("%d",&key);
printf(" ");
InsertAndOutput(key,H);
}
printf("\n");
}
5-42 整型关键字的散列映射
最新推荐文章于 2020-06-14 10:57:20 发布