ハッシュ表実験C言語版実現
6537 ワード
/*
C
*/
#include
#include
#define NULLKEY 0 // 0
#define N 10 //
typedef int KeyType;//
typedef struct
{
KeyType key;
int ord;
}ElemType; //
//
int hashsize[]={11,19,29,37}; // ,
int m=0; // ,
typedef struct
{
ElemType *elem; // ,
int count; //
int sizeindex; // hashsize[sizeindex]
}HashTable;
#define SUCCESS 1
#define UNSUCCESS 0
#define DUPLICATE -1
//
int InitHashTable(HashTable *H)
{
int i;
(*H).count=0; // 0
(*H).sizeindex=0; // hashsize[0]
m=hashsize[0];
(*H).elem=(ElemType*)malloc(m*sizeof(ElemType));
if(!(*H).elem)
exit(0); //
for(i=0;i (*H).elem[i].key=NULLKEY; //
return 1;
}
// H
void DestroyHashTable(HashTable *H)
{
free((*H).elem);
(*H).elem=NULL;
(*H).count=0;
(*H).sizeindex=0;
}
// (m , )
unsigned Hash(KeyType K)
{
return K%m;
}
//
void collision(int *p,int d) //
{
*p=(*p+d)%m;
}
// 9.17
// H K , , p
// , SUCCESS; , p , UNSUCCESS
// c , , 。
int SearchHash(HashTable H,KeyType K,int *p,int *c)
{
*p=Hash(K); //
while(H.elem[*p].key!=NULLKEY&&!(K == H.elem[*p].key))
{
// .
(*c)++;
if(*c collision(p,*c); // p
else
break;
}
if (K == H.elem[*p].key)
return SUCCESS; // ,p
else
return UNSUCCESS; // (H.elem[p].key==NULLKEY),p
}
int InsertHash(HashTable *,ElemType); //
//
void RecreateHashTable(HashTable *H) //
{
int i,count=(*H).count;
ElemType *p,*elem=(ElemType*)malloc(count*sizeof(ElemType));
p=elem;
printf("
");
for(i=0;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(0); //
(*H).elem=p;
for(i=0;i (*H).elem[i].key=NULLKEY; // ( )
for(p=elem;p InsertHash(H,*p);
}
// 9.18
// e H , 1;
// , 。
int InsertHash(HashTable *H,ElemType e)
{
int c,p;
c=0;
if(SearchHash(*H,e.key,&p,&c)) // e
return DUPLICATE;
else if(c {
// e
(*H).elem[p]=e;
++(*H).count;
return 1;
}
else
RecreateHashTable(H); //
return 0;
}
//
void TraverseHash(HashTable H,void(*Vi)(int,ElemType))
{
int i;
printf(" 0~%d
",m-1);
for(i=0;i if(H.elem[i].key!=NULLKEY) //
Vi(i,H.elem[i]);
}
// H K , , p
// , SUCCESS; , UNSUCCESS
int Find(HashTable H,KeyType K,int *p)
{
int c=0;
*p=Hash(K); //
while(H.elem[*p].key!=NULLKEY&&!(K == H.elem[*p].key))
{ // .
c++;
if(c collision(p,c); // p
else
return UNSUCCESS; // (H.elem[p].key==NULLKEY)
}
if (K == H.elem[*p].key)
return SUCCESS; // ,p
else
return UNSUCCESS; // (H.elem[p].key==NULLKEY)
}
void print(int p,ElemType r)
{
printf("address=%d (%d,%d)
",p,r.key,r.ord);
}
int 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, j, p;
KeyType k;
InitHashTable(&h);
for(i=0;i {
// N-1
j=InsertHash(&h,r[i]);
if(j==DUPLICATE)
printf(" %d , (%d,%d)
",
r[i].key,r[i].key,r[i].ord);
}
printf(" :
");
TraverseHash(h,print);
printf(" : ");
scanf("%d",&k);
j=Find(h,k,&p);
if(j==SUCCESS)
print(p,h.elem[p]);
else
printf("
");
j=InsertHash(&h,r[i]); // N
if(j==0) //
j=InsertHash(&h,r[i]); // N
printf(" :
");
TraverseHash(h,print);
printf(" : ");
scanf("%d",&k);
j=Find(h,k,&p);
if(j==SUCCESS)
print(p,h.elem[p]);
else
printf("
");
DestroyHashTable(&h);
system("pause");
return 0;
}
/*
:
60 , (60,9)
:
0~10
address=1 (1,5)
address=2 (2,6)
address=3 (3,7)
address=4 (4,8)
address=5 (60,2)
address=6 (17,1)
address=7 (29,3)
address=8 (38,4)
: 17
address=6 (17,1)
:
0~18
address=0 (38,4)
address=1 (1,5)
address=2 (2,6)
address=3 (3,7)
address=4 (4,8)
address=6 (60,2)
address=10 (29,3)
address=13 (13,10)
address=17 (17,1)
: 13
address=13 (13,10)
. . .
*/