[Data Structure Experiment] Search (1) Search algorithm based on hash table

1 Introduction

This experiment will implement a hash table-based search algorithm through C language

2. Experimental principle

2.1 Hash table

  Hash Table (Hash Table) is a common data structure that maps keys to a fixed-size array by using a hash function. This enables fast data lookup by calculating the hash value of the keyword and mapping it directly to the index of the array.

2.2 Linear detection method

  Hash function is the key component in a hash table, it takes a key and returns its index in an array. A good hash function should have the following properties:

  • Consistency: For the same input, always return the same output.
  • Uniformity: Hash values ​​are evenly distributed within the range of the array to avoid conflicts.

2.3 Conflict resolution

  Due to the limited output range of the hash function, different keywords may be mapped to the same index position, causing conflicts. There are many conflict resolution methods, including chain address method, open address method, etc.

3. Experiment content

3.1 Experimental questions

   Write an algorithm to construct the zipper list shown in Figure 8.47 in the textbook, output the singly linked list corresponding to each slot of the hash table, and program to calculate the average search length when the search is successful.

(1) Input requirements

  char *A[30]={
    
    
        "THE","OF","AND","TO","A",
        "IN","THAT","IS","WAS","HE",
        "FOR","IT","WITH","AS","HIS",
        "ON","BE","AT","BY","I",
        "THIS","HAD","NOT","ARE","BUT",
        "FROM","OR","HAVE","AN","THEY",
        };
    int B[30]={
    
    
        25,9,11,27,1,7,9,26,5,13,
        27,29,2,18,18,1,7,21,27,9,
        6,13,21,22,3,22,29,26,15,0
        };

(2) Output requirements

  1. Output the singly linked list corresponding to each slot HEADi of the hash table;
  2. Program to calculate and output the average search length when the search is successful.

3.2 Algorithm implementation

  1. Data structure definition:

    typedef struct P{
          
          
        char *data;
        struct P *next;
    }P;
    

       Defines a structureP, which contains a string type data fielddata and a pointer to the next node next, the basic node structure used to build the hash table.

  2. Hash table array:

    P* HEAD[32];
    

       Array Each element in HEAD is a pointer to the head of the linked list ~ This is a hash table with a total of 32 slots (buckets).

  3. Create function:

    void Create(char *A, int K)
    {
          
          
        int i = K;
        P *p = (P*)malloc(sizeof(P));
        p->data = A;
        p->next = HEAD[i];
        HEAD[i] = p;
    }
    

       CreateThe function is used to insert data into a hash table. Given string A and integer K, calculate the index of the array according to K and insert the data into the head of the corresponding linked list .

  4. Output function:

    void Output()
    {
          
          
        P *p;
        int i;
        for (i = 0; i < 32; i++)
        {
          
          
            printf("HEAD: %2d", i);
            if (HEAD[i] != NULL)
            {
          
          
                p = HEAD[i];
                for (; p != NULL; p = p->next)
                    printf(" —>%s", p->data);
            }
            else printf("空");
            printf("\n");
        }
    }
    

       OutputFunction is used to output the contents of the entire hash table. For each slot, output all nodes in the linked list.

  5. Find function:

    int Find(char *ch, int K){
          
          
        int time = 0;
        int i = K;
        P *p = HEAD[i];
        while (p){
          
          
            time++;
            if (p->data == ch) 
                return time;
            p = p->next;
        }
        return 0;
    }
    

       FindThe function is used to find specific data in a hash table. Given a string ch and an integer K, calculate the index of the array based on K and then find the string in the corresponding linked list. If found, returns the number of searches; otherwise, returns 0.

  6. Main function:

    int main()
    {
          
          
        // 数据初始化
        char *A[30] = {
          
           /* ... */ };
        int B[30] = {
          
           /* ... */ };
    
        int i, f, times = 0;
        float sum = 0;
    
        // 创建散列表
        for (i = 0; i < 30; i++){
          
          
            Create(A[i], B[i]);
        }
    
        // 输出散列表
        Output();
    
        // 查找并计算平均查找长度
        for (i = 0; i < 30; i++){
          
          
            f = Find(A[i], B[i]);
            if (f){
          
          
                times++;
                sum += f;
            }
        }
        
        printf("查找成功时的平均查找长度为:%f", sum / times);
    
        return 0;
    }
    

3.3 Code integration

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct P{
    
    
    char *data;
    struct P *next;
}P;
P* HEAD[32];
void Create(char *A,int K)
{
    
    
	int i=K;
    P *p=(P*)malloc(sizeof(P));
    p->data=A;
    p->next=HEAD[i];
    HEAD[i]=p;
   // printf("%d %s\n",i,p->A);
}
void Output()
{
    
    
    P *p;
    int i;
    for(i=0;i<31;i++)
    {
    
    
    	printf("HEAD: %2d",i);
        if(HEAD[i]!=NULL)
        {
    
    
            p=HEAD[i];
            for(;p!=NULL;p=p->next)
                printf(" —>%s",p->data);
        }
        else printf("空");
		printf("\n");
    }
}
int Find(char *ch,int K){
    
    
	int time=0;
	int i=K;
	P *p=HEAD[i];
	while(p){
    
    
		time++;
		if(p->data==ch) return time;
		p=p->next;
	}
	return 0;
}
int main()
{
    
    
    char *A[30]={
    
    
	"THE","OF","AND","TO","A",
	"IN","THAT","IS","WAS","HE",
	"FOR","IT","WITH","AS","HIS",
	"ON","BE","AT","BY","I",
	"THIS","HAD","NOT","ARE","BUT",
	"FROM","OR","HAVE","AN","THEY",
	};
	int B[30]={
    
    
	25,9,11,27,1,7,9,26,5,13,
	27,29,2,18,18,1,7,21,27,9,
	6,13,21,22,3,22,29,26,15,0
	};
    int i,f,times=0;
    float sum=0;
    for(i=0;i<30;i++){
    
    
    	Create(A[i],B[i]);
	}
    Output();
    for(i=0;i<30;i++){
    
    
    	f=Find(A[i],B[i]);
    	if(f){
    
    
    		//printf("查找成功");
    		times++;
    		sum+=f;
		}
    	//else  printf("查找失败");
	}
	printf("查找成功时的平均查找长度为:%f",sum/times);

    return 0;
}

4. Experimental results

Insert image description here

HEAD:  0>THEY
HEAD:  1>ON —>A
HEAD:  2>WITH
HEAD:  3>BUT
HEAD:  4空
HEAD:  5>WAS
HEAD:  6>THIS
HEAD:  7>BE —>IN
HEAD:  8空
HEAD:  9>I —>THAT —>OF
HEAD: 10空
HEAD: 11>AND
HEAD: 12空
HEAD: 13>HAD —>HE
HEAD: 14空
HEAD: 15>AN
HEAD: 16空
HEAD: 17空
HEAD: 18>HIS —>AS
HEAD: 19空
HEAD: 20空
HEAD: 21>NOT —>AT
HEAD: 22>FROM —>ARE
HEAD: 23空
HEAD: 24空
HEAD: 25>THE
HEAD: 26>HAVE —>IS
HEAD: 27>BY —>FOR —>TO
HEAD: 28空
HEAD: 29>OR —>IT
HEAD: 30空
查找成功时的平均查找长度为:1.466667

Guess you like

Origin blog.csdn.net/m0_63834988/article/details/134676919