哈希查找的问题
代码:#include <stdio.h>#include <stdlib.h>
#include <math.h>
#define OK 1
#define ERROR 0
typedef int status;
#define NULLKEY 0
#define N 10
typedef int keytype;
typedef struct
{
keytype key;
int ord;
}Elemtype;
#define EQ(a,b) ((a)==(b))
int hashsize[]={11,19,29,37};
int m=0;
typedef struct
{
Elemtype *elem;
int count;
int sizeindex;
}Hashtable;
#define success 1
#define unsuccess 0
#define duplicate -1
status Lnithashtable(Hashtable *h)
{
int i;
(*h).count=0
(*h).sizeindex=0;
m=hashsize[0];
(*h).elem=(Elemtype *)malloc(m*sizeof(Elemtype));
if(!(*h).elem)
exit(OVERFLOW);
for(i=0;i<m;i++)
(*h).elem[i].key=NULLKEY;
return OK;
}
void Destroyhashtable(Hashtable *h)
{
free((*h).elem);
(*h).elem=NULL;
(*h).count=0;
(*h).sizeindex=0;
}
unsigned Hash(keytype k)
{
return k%m;
}
void collision(int *p,int d)
{
*p=(*p+d)%m;
}
status Searchhash(Hashtable h,keytype k,int *p,int *c)
{
*p=Hash(k);
while(h.elem[*p].key!=NULLKEY&&!EQ(k,h.elem[*p].key))
{
(*c)++;
if(*c<m)
collision(p,*c);
else
break;
}
if EQ(k,h.elem[*p].key)
return success;
else
return unsuccess;
}
status Inserthash(Hashtable *h,Elemtype e);
void Recreatehashtable(Hashtable *h)
{
int i,count=(*h).count;
Elemtype *p,*elem=(Elemtype *)malloc(count *sizeof(Elemtype));
p=elem;
printf("重建哈希表\n");
for(i=0;i<m;i++)
if(((*h).elem+i)->key!=NULLKEY);
*p++=((*h).elem+i);
(*h).count=0;
(*h).sizeindex++;
m=hashsize[(*h).sizeindex];
p=(Elemtype *)realloc((*h).elem,m*sizeof(Elemtype));
if(!p)
exit(OVERFLOW);
(*h).elem=p;
for(i=0;i<m;i++)
(*h).elem[i].key=NULLKEY;
for(p=elem;p<elem+count;p++)
Inserthash(h,*p);
}
status Inserthash(Hashtable *h,Elemtype e)
{
int c,p;
c=0;
if(Searchhash(*h,e.key,&p,&c))
return duplicate;
else if(c<hashsize[(*h).sizeindex]/2)
{
(*h).elem[p]=e;
++(*h).count;
return OK;
}
else
Recreatehashtable(h);
return ERROR;
}
void Traversehash(Hashtable h,void(*Vi)(int,Elemtype))
{
int i;
printf("哈希地址0~%d\n",m-1);
for(i=0;i<m;i++)
if(h.elem[i].key!=NULLKEY)
Vi(i,h.elem[i]);
}
status Find(Hashtable h,keytype k,int *p)
{
int c=0;
*p=Hash(k);
while(h.elem[*p].key!=NULLKEY&&!EQ(k,h.elem[*p].key))
{
c++;
if(c<m)
collision(p,c);
else
return success;
}
if(EQ(k,h.elem[*p].key))
return success;
else
return unsuccess;
}
void print(int p,Elemtype r)
{
printf("address=%d(%d,%d)\n",p,r.key,r.ord);
}
void main()
{
Elemtype r[N]={{17,1},{60,2},{29,3},{38,4},{1,5},{2,6},{3,7},{4,8},{60,9},{13,10}};
Hashtable h;
int i,p;
status j;
keytype k;
Lnithashtable(&h);
for(i=0;i<N-1;i++)
{
j=Inserthash(&h,r[i]);
if(j==duplicate)
printf("表中已有关键字为%d录,无法再插入记录(%d,%d)\n",r[i].key,r[i].key,r[i].ord);
}
printf("按哈希地址的顺序遍历哈希表:\n");
Traversehash(h,print);
printf("请输入待查找记录的关键字: ");
scanf("%d",&k);
j=Find(h,k,&p);
if(j=success)
print(p,h.elem[p]);
else
printf("没找到\n");
j=Inserthash(&h,r[i]);
if(j==ERROR)
j=Inserthash(&h,r[i]);
printf("按哈希地址的顺序遍历重建后的哈希表: \n");
Traversehash(h,print);
printf("请输入待查找的关键字: ");
scanf("%d",&k);
j=Find(h,k,&p);
if(j==success)
print(p,h.elem[p]);
else
printf("没找到\n");
Destroyhashtable(&h);
}
问题:--------------------Configuration: pl - Win32 Debug--------------------
Compiling...
pl.cpp
H:\C语言编程实践文件夹\pl\pl.cpp(32) : error C2228: left of '.sizeindex' must have class/struct/union type
H:\C语言编程实践文件夹\pl\pl.cpp(86) : warning C4390: ';' : empty controlled statement found; is this the intent?
H:\C语言编程实践文件夹\pl\pl.cpp(86) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'Elemtype *' (or there is no acceptable conversion)
执行 cl.exe 时出错.
pl.exe - 1 error(s), 0 warning(s)