Simple construction of hash tables and zipper-style conflict resolution (simplified implementation)

Simple construction of hash tables and zipper-style conflict resolution

hash table

insert image description here

It is used to store elements of a specific form and store them in different key values. For example, in an array {1, 2, 3, 3, 4}, we want to count the number of each element, you can create an array a[5], a[0] represents the number of 0s a[1] represents the number of 1s..., the size of the array depends on the maximum and minimum values ​​in the array, if the maximum value is very large, there is no way to create a hash table like this, so we You can create a small hash table. How to represent large data? You can take the remainder of the large data and the length of the hash table, but there may be conflicts between elements at this time, and the two elements occupy the same one location, so there's a zipper for conflict resolution

Zipper conflict resolution

Here each node of the hash table is defined as a singly linked list, and then if there is a conflicting element, the element is stored at the head of the linked list

code section

1. Definition of singly linked list

The member needs to have a value of itself, representing the value of the element, and a pointer to the next node

struct ListNode		//单链表 
{
    
    
	int val;
	ListNode *next;
	ListNode(int x):val(x),next(NULL) {
    
    };	
};
2. Find the key value of the element

Directly take the remainder of the current element and the length of the hash table

int hash_func(int key,int table_len)	//返回当前元素应该在的位置 
{
    
    
	return key%table_len;
}
3. Header insertion method inserts elements into the hash table

Each insert inserts the position of the head in the linked list, and then updates the head node

//头插法 
void insert(ListNode *hash_table[],ListNode *node,int table_len)
{
    
    
	int hash_key=hash_func(node->val,table_len);
	node->next=hash_table[hash_key];
	hash_table[hash_key]=node;
}
4. Find elements

First find the key value of the element, then query in the corresponding singly linked list, the query returns true, and at the end no query returns false

//查找 
bool search(ListNode *hash_table[],int value,int table_len)
{
    
    
	int hash_key=hash_func(value,table_len);
	ListNode *head=hash_table[hash_key];
	while(head)
	{
    
    
		if(head->val==value)
			return true;
		head=head->next;
	}
	return false;

5. Main function part (for testing)

Create a variable of type vector<ListNode *> to convert all integers in the array (convert to ListNode * type), then use the written insert function to insert all elements into the hash table, and then print All elements in the hash table.
Test the lookup function, look up 0~9 in the hash table

int main (void)
{
    
    
	const int table_len=11;
	ListNode *hash_table[table_len]={
    
    0};
	vector<ListNode *> hash_node_vec;
	int test[8]={
    
    1,1,4,9,20,30,150,500};
	for(int i=0;i<8;i++)
		hash_node_vec.push_back(new ListNode(test[i]));
		
	for(int i=0;i<hash_node_vec.size();i++)
	{
    
    
		insert(hash_table,hash_node_vec[i],table_len);
	}
	
	//打印哈希表
	cout<<"Hash table\n";
	for(int i=0;i<table_len;i++)
	{
    
    
		printf("[%d]",i);
		ListNode *head=hash_table[i];
		while(head)
		{
    
    
			printf("->%d",head->val);
			head=head->next;
		}
		cout<<endl;
	}
	cout<<endl;
	
	cout<<"Test search(0~9)\n"; 
	for(int i=0;i<10;i++)
	{
    
    
		if(search(hash_table,i,table_len))
		{
    
    
			cout<<i<<" is in the hash table.\n";
		}
		else
		{
    
    
			cout<<i<<" is not in the hash table.\n";
		}
	}
	
	return 0;
}

insert image description here

Summarize

In many algorithm problems, hash tables are frequently used, and hash tables can be used to store data to avoid repeated operations and achieve the purpose of changing space for time

Guess you like

Origin blog.csdn.net/weixin_46035615/article/details/123920203