C language to implement hash table (open address method)

The author recently learned the hash table in the data structure and implemented it simply in C language.
This blog is designed to exchange ideas.

This blog introduces a hash table constructed by the open address method

The principle of hash table can refer to this article: Introduction to the principle of hash table

If you want to refer to the hash table constructed by the zipper method, please refer to this article: C language to implement the hash table (zipper method)

Structure description

typedef struct element
{
	int key;		// key值  唯一不重复 
	int data;		// data 域  
}Element;

The data field can be changed to a custom structure (of course, part of the code of the subsequent main function needs to be modified accordingly) The
key value is calculated by the hash function and a mapped address is obtained

typedef struct table
{
	Element **elm;	//  
	int count;		// 记录表中记录的数据个数 
	int size;	// 哈希表的大小 = SIZE = 50
}Table;

Note: elm is a pointer to a pointer array of Element structures (please understand well), that is, a pointer array of Element type is created, and elm points to the head address of this array, so it is declared as a secondary pointer (pointer to pointer) .

Part of the function description:
create a hash table function:

Table * init_Table()
{
	Table *t = (Table*)malloc(sizeof(Table));
	t->size = REMAINDER;
	t->count = 0;
	t->elm = (Element **)malloc(SIZE*sizeof(Element*));
	/*
	创建一个长度为13的指针数组,类型为Element*
	并将数组头地址赋值给 elm 的二级指针 
	*/ 
	int i;
	for(i=0 ; i<t->size ; i++)
	{
		t->elm[i] = NULL;	// 将数组的每个域赋空 
	}	
	return t;
}

Declare a variable of type Table, create space for it, and return its address.

During initialization, pay attention to the initialization of variables of the pointer type (some compilers will help you to empty, some will not)

Insert function

void Insert(Table *t,Element * k)		//将key值插入到哈希表中 
{
	int i;
	int position;
	position = hash(k->key);
	
	for(i=1 ; t->elm[position]!=NULL && i<t->size ;i++ )
	{
		position=(hash(position)+i)%t->size;
	}
	t->elm[position] = k;	
	t->count += 1;
	return ;
}

The general form of the open address method is:
H (key) is a hash function, and m is the length of the table.
This method is easy to cause stacking. The solution is to improve the hash function or use the zipper method (each has advantages and disadvantages).

Find function:

int serch(Table *t, Element * k)		//查找 value 并返回其所在的地址 
{
	int position=hash(k->key);
	while(t->elm[position]->key != k->key)
	{
		position = hash(position+1);
		if(t->elm[position]==NULL || position == hash(k->key))
		/*
		出现以下几种情况即判断查找失败
		1.对应的 position 位置的地址为空 
		2.遍历整个表都没有对应的 key 值 
		*/ 
		return -1;
	}
	
	return position;	
}

The following situations occur when the search fails
1. The address of the corresponding position is empty
2. There is no corresponding key value for traversing the entire table

Complete code:

#include<stdio.h>
#include<stdlib.h>
#define REMAINDER 13
#define SIZE 50 

typedef struct element
{
	int key;		// key值  唯一不重复 
	int data;		// data 域  
}Element;

typedef struct table
{
	Element **elm;	//  
	int count;		// 记录表中记录的数据个数 
	int size;	// 哈希表的大小 = SIZE = 50
}Table;

int hash(int key)
{
	return key%REMAINDER;	
}

Table * init_Table()
{
	Table *t = (Table*)malloc(sizeof(Table));
	t->size = SIZE;
	t->count = 0;
	t->elm = (Element **)malloc(SIZE*sizeof(Element*));
	/*
	创建一个长度为13的指针数组,类型为Element* 
	并将数组头地址赋值给 elm 的二级指针 
	*/ 
	int i;
	for(i=0 ; i<t->size ; i++)
	{
		t->elm[i] = NULL;	// 将数组的每个域赋空 
	}	
	return t;
}

void Insert(Table *t,Element * k)		//将key值插入到哈希表中 
{
	int i=0;
	int position;
	position = hash(k->key);
	
	for(i=1 ; t->elm[position]!=NULL && i<t->size ;i++ )
	{
		position=(hash(position)+i)%t->size;
	}
	t->elm[position] = k;	
	t->count += 1;
	return ;
}

int serch(Table *t, Element * k)		//查找 value 并返回其所在的地址 
{
	int position=hash(k->key);
	while(t->elm[position]->key != k->key)
	{
		position = hash(position+1);
		if(t->elm[position]==NULL || position == hash(k->key))
		/*
		出现以下几种情况即判断查找失败
		1.对应的 position 位置的地址为空 
		2.遍历整个表都没有对应的 key 值 
		*/ 
		return -1;
	}
	
	return position;	
}


void Print_Table(Table *t)		//打印部分哈希表 
{
	int i;
	for(i=0 ; i<13 ; i++)
	{
		if(t->elm[i])
		printf("[%d %d] ",t->elm[i]->key , t->elm[i]->data);
		else printf("NULL ");
	}	
	printf("\n");
}

int main()
{
	Table *t = init_Table();
	
	Element a[]={{12,99},{13,98},{26,87},{14,77},{15,100},{10,59}};
	int length = sizeof(a)/sizeof(Element);
	
	int i;
	for(i=0 ; i<length ; i++)
	{
		Insert(t,&a[i]);
	}
	Print_Table(t);
	printf("a[3] is locat %d\n",serch(t,&a[3]));
	free(t);
}


Run screenshot:
Insert picture description here

Thanks for watching ~

Published 7 original articles · won 12 · views 774

Guess you like

Origin blog.csdn.net/M_H_T__/article/details/103263414