#include <stdio.h>
#include <stdlib.h>
int hash(int key,int P){ //哈希函数
return key%P;
}
/*
enum Entry{ //枚举类型
leg,Empty //占位情况
};
*/
struct HashElm{ //哈希表元素
int elem; //key
//enum Entry info;
int state; //状态:0表空,1表示占用
};
typedef struct HashTbl{ //哈希表结构
int tablesize; //表长
struct HashElm *Tcell; //表元素
}*HashTable;
HashTable Initialize(int Tablesize){ //初始化哈希表
HashTable H= malloc(sizeof(struct HashTbl));
H->tablesize=Tablesize;
H->Tcell=malloc(sizeof(struct HashElm)*H->tablesize);
while(Tablesize)
H->Tcell[--Tablesize].state=0;
return H;
}
int Find(int key,HashTable H) //找到key所在位置
{
int position=hash(key,H->tablesize);
while((H->Tcell[position].state==1)&&(H->Tcell)[position].elem!=key)
//1、位置被占用
//2、重复查询
//1&2才需要解决冲突
{
position++; //线性探测法
if(position==H->tablesize)
position-=H->tablesize; //越界处理 表长10 H[10]越界->H[0]
}
return position;
}
void InandOut(int key,HashTable H) //插入表元素,输出位置
{
int position=Find(key,H);
if(H->Tcell[position].state==0)
{
H->Tcell[position].elem=key;
H->Tcell[position].state=1; //表示已经占用了
}
printf("%d",position);
}
int main(int argc,char *argv[])
{
int n,p,key,i;
HashTable H;
scanf("%d %d",&n,&p);
H=Initialize(p);
scanf("%d",&key);
InandOut(key,H);
for(i=1;i {
scanf("%d",&key);
printf(" ");
InandOut(key,H);
}
return 0;
}