Hash table construction and search algorithm

To realize the construction and search algorithm of hash table, we need to construct hash function with the method of division and reservation remainder, and solve the conflict with one detection and then hash, two detection and then hash.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*typedef struct {
	ElemType *elem;
	int count;
	int sizeindex;
}HashTable;*/
typedef struct{
    int key;
}keytype;

typedef struct { 
    keytype elem[100];
    int length;   /*Current length*/
    int size;  /*Total length of hash table*/
}hashtable; 
int a=0,b=0,select;
int hash2(int i,int t)
{  if(i%2==0)
     t=t+pow(++a,2);
   else
     t=t-pow(++b,2);
   return t;
}
int hash1(int i,int t)
{ 
i++;
 t=i;
   return t;
}
int hash(hashtable h,int k)
{
    return  k%(h.size);
}
void creat(hashtable *h)
{  int i,j,key,t,p;
   printf("Please enter the length of the hash table and the record length in the table:");
   scanf("%d%d",&h->size,&h->length);
   printf("Please choose:\n1. Using linear detection to rehash conflict\n2. Using second detection to rehash conflict"); 
   scanf("%d",&select);
   for(i=0;i<h->size;i++)//Initialization sets all the keywords in the hash table to - 1, which means the storage bit is empty. 
   h->elem[i].key=-1;
   printf("input data:\n");
   for(j=0;j<h->length;j++)
    {  scanf("%d",&key);
       p=hash(*h,key);
       if(h->elem[p].key==-1)
         h->elem[p].key=key;
       else
         {  i=0;
            t=p;
            while(h->elem[p].key!=-1&&h->elem[p].key!=key&&i<h->size/2)
              {  
              if(select==2){
			  
			  p=hash2(i,t);
                 i++;
             }else if(select==1){
			 
             p=hash1(i,t);
                 i++;
             }
              }
            a=b=0;     //This record finds the storage location, and sets the a and b parameters to 0 to prepare for the next record conflict. 
            h->elem[p].key=key;
         }
     }
}
int SearchHash(hashtable H,int k){
        int  p,i=0,t=0;
	p=hash(H,k);                                //Find the address of Hash 
	while(H.elem[p].key!=-1&&(k!=H.elem[p].key))//There are records in the address and the keywords are not equal 
	 {
	 	if(select==2){
		 
	 p=hash2(i,t);                            // The address of the next probe obtained by the second probe method 
	 i++;   
         }else if(select==1){
         	 p=hash1(i,t);                            // The address of the next probe obtained by the second probe method 
	         i++;
		 }
    }
	if(k==H.elem[p].key)
	return p;
	else
	return -1;
	
}

void printhash(hashtable *h)

{  int i;

   for(i=0;i<h->size;i++)

    printf("%-4.2d",i);

     printf("\n");

    for(i=0;i<2*h->size;i++)

    printf("--");

    printf("\n");

    for(i=0;i<h->size;i++)

    printf("%-4.2d",h->elem[i].key);

}
int main()

{   hashtable t;
    int i,key,key1,c;
    creat(&t);
    printf("Show hash table:\n\n");
    printhash(&t);
    printf("\n\n The current hash table record length is:%d\n",t.length);
    printf("\n Please enter the key to find the record:");
    scanf("%d",&key);
    c=SearchHash(t,key);
    if(c!=-1)
      printf("The record's seat is:%d\n",c);
    else
      printf("The record was not found!\n");
      
      
      
      
      return 0;

}

The test results are as follows:

Please enter the length of the hash table and the record length in the table: 30 10
 Please choose:
1. Using linear detection to rehash conflict
 2. Using second detection to rehash conflict 2
input data:
12 13 14 56 25 35 36 38 37 12
 Show hash table:

00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  
------------------------------------------------------------------------------------------------------------------------
-01 -01 -01 -01 -01 35  36  37  38  -01 -01 -01 12  13  14  -01 -01 -01 -01 -01 -01 -01 -01 -01 -01 25  56  -01 -01 -01 

The current hash table record length is: 10

Please enter the keyword to find the record: 14
 The position of the record is: 14

 

Added by ade1982 on Fri, 25 Oct 2019 18:43:06 +0300