Hash table experiment C language version implementation

  • 2020-04-02 01:15:18
  • OfStack



#include <stdio.h>
#include <malloc.h>
#define NULLKEY 0 //0 is no record flag
#define N 10  //Number of data elements
typedef int KeyType;//Let the keyword field be an integer
typedef struct
{
 KeyType key;
 int ord;
}ElemType; //Data element type
//Open the storage structure of the addressable hash table
int hashsize[]={11,19,29,37}; //Hash table capacity increasing table, an appropriate sequence of prime Numbers
int m=0; //Hash table length, global variable
typedef struct
{
 ElemType *elem; //Data elements store base addresses and dynamically allocate arrays
 int count; //Current number of data elements
 int sizeindex; //Hashsize [sizeindex] is the current capacity
}HashTable;
#define SUCCESS 1
#define UNSUCCESS 0
#define DUPLICATE -1
//Construct an empty hash table
int InitHashTable(HashTable *H)
{  
 int i;
 (*H).count=0; //The current number of elements is 0
 (*H).sizeindex=0; //The initial storage capacity is hashsize[0]
 m=hashsize[0];
 (*H).elem=(ElemType*)malloc(m*sizeof(ElemType));
 if(!(*H).elem)
  exit(0); //Memory allocation failed
 for(i=0;i<m;i++)
  (*H).elem[i].key=NULLKEY; //Mark of an unfilled record

 return 1;
}
//& have spent Destroy hash table H
void DestroyHashTable(HashTable *H)
{ 
 free((*H).elem);
 (*H).elem=NULL;
 (*H).count=0;
 (*H).sizeindex=0;
}
//A simple hash function (m for table length, global variable)
unsigned Hash(KeyType K)
{ 
 return K%m;
}
//Open addressing handles conflicts
void collision(int *p,int d) //Linear detection and hash
{  
 *p=(*p+d)%m;
}
//Algorithm 9.17
//Find the element with key code K in the open addressing hash table H. If the search is successful, p indicates the pending data
//Element in the table and returns SUCCESS; Otherwise, p indicates the insertion location and returns UNSUCCESS
//C is used to count the number of conflicts, and its initial value is set to zero for reference when inserting in the table.
int SearchHash(HashTable H,KeyType K,int *p,int *c)
{
 *p=Hash(K); //Find the hash address
 while(H.elem[*p].key!=NULLKEY&&!(K == H.elem[*p].key))
 {
  //There are records in this position. And the keywords are not equal
  (*c)++;
  if(*c<m)
   collision(p,*c); //Find the next probe address, p
  else
   break;
 }
 if (K == H.elem[*p].key)
  return SUCCESS; //Successful search, p returns the location of the data element to be searched
 else
  return UNSUCCESS; //The search failed (h.lem [p].key==NULLKEY), and p returns the insertion position
}
int InsertHash(HashTable *,ElemType); //Declaration of a function
//Rebuild the hash table
void RecreateHashTable(HashTable *H) //Rebuild the hash table 
{ 
 int i,count=(*H).count;
 ElemType *p,*elem=(ElemType*)malloc(count*sizeof(ElemType));
 p=elem;
 printf(" Rebuild the hash table n");
 for(i=0;i<m;i++) //Save the original data to elem
  if(((*H).elem+i)->key!=NULLKEY) //This unit has data
   *p++=*((*H).elem+i);
 (*H).count=0;
 (*H).sizeindex++; //Increase storage capacity
 m=hashsize[(*H).sizeindex];
 p=(ElemType*)realloc((*H).elem,m*sizeof(ElemType));
 if(!p)
  exit(0); //Memory allocation failed
 (*H).elem=p;
 for(i=0;i<m;i++)
  (*H).elem[i].key=NULLKEY; //Flags for unfilled records (initialization)
 for(p=elem;p<elem+count;p++) //Insert the original data into the reconstructed hash table according to the new table length
  InsertHash(H,*p);
}
//Algorithm 9.18
//If the search fails, insert the data element e into the open addressing hash table H, and return 1;
//If the number of conflicts is too large, the hash table is rebuilt.
int InsertHash(HashTable *H,ElemType e)
{
 int c,p;
 c=0;
 if(SearchHash(*H,e.key,&p,&c)) //There are already elements in the table that have the same keyword as e
  return DUPLICATE;
 else if(c<hashsize[(*H).sizeindex]/2) //Number of conflicts c does not reach the upper limit,(the threshold of c is adjustable)
 {
  //Insert the e
  (*H).elem[p]=e;
  ++(*H).count;
  return 1;
 }
 else
  RecreateHashTable(H); //Rebuild the hash table 

 return 0;
}
//The hash table is traversed in the order of hash addresses
void TraverseHash(HashTable H,void(*Vi)(int,ElemType))
{  
 int i;
 printf(" Hash address 0 ~ %dn",m-1);
 for(i=0;i<m;i++)
  if(H.elem[i].key!=NULLKEY) //There are data
   Vi(i,H.elem[i]);
}
//Find the element with key code K in the open addressing hash table H. If the search is successful, p indicates the pending data
//Element in the table and returns SUCCESS; Otherwise, return UNSUCCESS
int Find(HashTable H,KeyType K,int *p)
{
 int c=0;
 *p=Hash(K); //Find the hash address
 while(H.elem[*p].key!=NULLKEY&&!(K == H.elem[*p].key))
 { //There are records in this position. And the keywords are not equal
  c++;
  if(c<m)
   collision(p,c); //Find the next probe address, p
  else
   return UNSUCCESS; //Invalid search (h.lem [p]. Key ==NULLKEY)
 }
 if (K == H.elem[*p].key)
  return SUCCESS; //Successful search, p returns the location of the data element to be searched
 else
  return UNSUCCESS; //Invalid search (h.lem [p]. Key ==NULLKEY)
}
void print(int p,ElemType r)
{
 printf("address=%d (%d,%d)n",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;i++)
 {
  //Insert the first N-1 records
  j=InsertHash(&h,r[i]);
  if(j==DUPLICATE)
   printf(" The existing keyword in the table is %d Could not insert a record (%d,%d)n",
    r[i].key,r[i].key,r[i].ord);
 }
 printf(" The hash table is traversed in the order of hash addresses :n");
 TraverseHash(h,print);
 printf(" Please enter the keyword for the record to find : ");
 scanf("%d",&k);
 j=Find(h,k,&p);
 if(j==SUCCESS)
  print(p,h.elem[p]);
 else
  printf(" Did not find n");
 j=InsertHash(&h,r[i]); //Insert the NTH record
 if(j==0) //Rebuild the hash table 
  j=InsertHash(&h,r[i]); //Rebuild the hash table After reinserting the first N A record  
 printf(" Traverse the reconstructed hash table in the order of hash addresses :n");
 TraverseHash(h,print);
 printf(" Please enter the keyword for the record to find : ");
 scanf("%d",&k);
 j=Find(h,k,&p);
 if(j==SUCCESS)
  print(p,h.elem[p]);
 else
  printf(" Did not find n");
 DestroyHashTable(&h);

 system("pause");
 return 0; 
}
 

Related articles: