#include<stdio.h>
#include<stdlib.h>
#define MAX 20
typedef struct
{
int num;
char name[20];
}ElemType;
typedef struct
{
ElemType *elem;
int count;
int sizeindex;
}HashTable;
int Hash(int num)
{
int p;
p=num%5;
return p;
}
void lnitHash(HashTable *H)
{
int i;
H->elem=(ElemType *)malloc(MAX*sizeof(ElemType));
H->count=0;
H->sizeindex=MAX;
for(i=0;i<MAX;i++)
H->elem[i].num=0;
}
int SearHash(HashTable H,int key,int *p)
{
int c=0;
*p=Hash(key);
while(H.elem[*p].num!=key && H.elem[*p].num!=0)
{
c=c+1;
if(c%2==1)
*p=*p(c+1)*(c+1)/4;
else
*p=*p-(c*c)/4;
}
if(H.elem[*p].num==key)
return 1;
else
return 0;
}
void lnserHash(HashTable *H,ElemType e)
{
int p;
SearHash(*H,e.num,&p);
H->elem[p]=e;
++H->count;
}
void main()
{
HashTable H;
int p,key,i;
ElemType e;
lnitHash(&H);
for(i=0;i<3;i++)
{
loop;printf("输入第%d个学生学号\n",i+1);
scanf("%d",&e.num);
if(!SearHash(H,e.m=num,&p))
{
printf("输入第%d个学生名字\n",i+1);
scanf("%s",e.name);
lnsertHash(*H,e);
}
else
{
perintf("改学院已存在:\n");
goto loop;
}
}
printf("请输入你要查找的学生学号:\n");
scanf("%d",&key);
if(SearHash(H,key,&p))
{
printf("查找成功!学生的姓名是%s\n",H.elem[p].name);
printf("学生所在表中的位置是%d\n",p);
}
else
printf("查找失败!你要查找的学生不存在!\n");
}