Article directory
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
- Output the singly linked list corresponding to each slot HEADi of the hash table;
- Program to calculate and output the average search length when the search is successful.
3.2 Algorithm implementation
-
Data structure definition:
typedef struct P{ char *data; struct P *next; }P;
Defines a structure
P
, which contains a string type data fielddata
and a pointer to the next nodenext
, the basic node structure used to build the hash table. -
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). -
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; }
Create
The function is used to insert data into a hash table. Given stringA
and integerK
, calculate the index of the array according toK
and insert the data into the head of the corresponding linked list . -
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"); } }
Output
Function is used to output the contents of the entire hash table. For each slot, output all nodes in the linked list. -
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; }
Find
The function is used to find specific data in a hash table. Given a stringch
and an integerK
, calculate the index of the array based onK
and then find the string in the corresponding linked list. If found, returns the number of searches; otherwise, returns 0. -
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
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