Hash table implementation

List an array of pointers to achieve hash table

Hash table, also known as hash table in order to achieve faster search, which uses an array of properties, ideally (no conflict, in fact, impossible) time complexity O (1) can be achieved.
Given article use an array to store the list of ways to avoid conflicts described more limited set of sets.

Hash table data structure to achieve

  1. Structure is defined
typedef struct Node{
	int data;
	struct Node* next;
}ListNode;

struct HashTable{
	ListNode* elem[SIZE];
	int count;
}ht;

Definition of two structures, an array for storing a list head pointer (initially a null pointer),
and count the number of data stored in the hash table.

  1. Hash function to achieve
#define SIZE 20
#define NODESIZE 200

We define the number list (array length) is 20, then if the hash function to achieve divisible (address value (array subscript) mapping.)
May be another hash function to the following:

int CalHash(int key){
	return key%(SIZE-1; //宏定义SIZE为20
}

So that the corresponding data can be distributed more evenly in the hash table to improve search efficiency (the book says so).

3. hash table initialization
because the array are stored in each node an address, we will each array element is initialized to a NULL pointer to the insertion element, that receives the address pointer returned by the malloc function.

void InitializeHash(){
	ht.count=0;
	for(int i=0;i<SIZE;i++)
		ht.elem[i]=NULL;	
}

Hash table search

Function prototype

int SearchHash(int key,int* s)

Search function nested within the insertion and deletion functions inside functions, array subscript CalHash transfer function calculated between functions, therefore two parameters passed to the function, to find a keyword, the other array is returned CalHash standard, with a transfer int *.
Detailed code

int SearchHash(int key,int* s){
	//(*s)=CalHash(key);
	ListNode* current=ht.elem[*s];
	while(current!=NULL){
		if(current->data==key)
			return 1;
		current=current->next;
	}
	return 0;
}

Hashtable insert

Function prototype

int InsertHash(int e)

E is the parameter value into the element.
Detailed code

int InsertHash(int e){
	int s;
	if(ht.count==NODESIZE){
		printf("哈希表已满\n");
		return 1;
	}
	else{
		s=CalHash(e);
		int r=SearchHash(e,&s);
		if(r){
			printf("所处链表有重复元素,无法插入\n");
			return 0;
		}
		else{
			if(ht.elem[s]==NULL){		//空链表
				(ht.elem[s])=(ListNode*) malloc(sizeof(ListNode));
				(ht.elem[s])->next=NULL;
				(ht.elem[s])->data=e;
				ht.count++;
				printf("链式哈希表第%d个链表插入头结点%d\n",s,ht.elem[s]->data);
			}
			else{						//尾插
				ListNode* current=(ht.elem[s]);
				while(current->next!=NULL)
					current=current->next;
				current->next=(ListNode*) malloc(sizeof(ListNode));
				current->next->data=e;
				current->next->next=NULL;
				printf("存在冲突,链式哈希表第%d个链表插入尾节点%d\n",s,current->next->data);
				ht.count++;
			}
		}
	}
	return 1;
}

Delete function

Function prototype

int DeleteHash(int key)

Detailed code

int DeleteHash(int key){
	int s=CalHash(key);
	int i=SearchHash(key,&s);
	if(i){
		ListNode* current=ht.elem[s];
		ListNode* prev=NULL;
		if(current->data==key){		//头结点删除
			ht.elem[s]=current->next;
			current->next=NULL;
			printf("哈希表第%d个链表删除头结点%d\n",s,current->data);
			free(current);
			ht.count--;
			return 1;
		}
		while(current!=NULL){		//后续节点删除
			if(current->data!=key){
				prev=current;
				current=current->next;
			}
			else
				break;
		}
		prev->next=current->next;
		current->next=NULL;
		printf("哈希表第%d个链表删除中间节点或尾结点%d\n",s,current->data);
		free(current);
		ht.count--;
		return 1;
	}
	else
		printf("你要删除的元素不存在\n");
	return 0;
}

free function

Function prototype

void freeHash()

Detailed code

void freeHash(){
	for(int i=0;i<SIZE;i++){
		ListNode* current=ht.elem[i];
		while(current!=NULL){
			ListNode* temp=current;
			current=current->next;
			temp->next=NULL;
			free(temp);
		}
	}
}

All codes

#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
#define NODESIZE 200
void freeHash();
int DeleteHash(int key);
int InsertHash(int e);
int CalHash(int key);
void InitializeHash();
int SearchHash(int key,int* s);


typedef struct Node{
	int data;
	struct Node* next;
}ListNode;

struct HashTable{
	ListNode* elem[SIZE];
	int count;
}ht;

int main(void){
	InitializeHash();
	int a1=InsertHash(1);
	int a2=InsertHash(2);
	int a3=InsertHash(21);
	int a4=DeleteHash(2);
	int a5=DeleteHash(21);
	freeHash();
	return 0;
}

int CalHash(int key){
	return key%SIZE;
}

void InitializeHash(){
	ht.count=0;
	for(int i=0;i<SIZE;i++)
		ht.elem[i]=NULL;	
}

int SearchHash(int key,int* s){
	//(*s)=CalHash(key);
	ListNode* current=ht.elem[*s];
	while(current!=NULL){
		if(current->data==key)
			return 1;
		current=current->next;
	}
	return 0;
}


int InsertHash(int e){
	int s;
	if(ht.count==NODESIZE){
		printf("哈希表已满\n");
		return 1;
	}
	else{
		s=CalHash(e);
		int r=SearchHash(e,&s);
		if(r){
			printf("所处链表有重复元素,无法插入\n");
			return 0;
		}
		else{
			if(ht.elem[s]==NULL){		//空链表
				(ht.elem[s])=(ListNode*) malloc(sizeof(ListNode));
				(ht.elem[s])->next=NULL;
				(ht.elem[s])->data=e;
				ht.count++;
				printf("链式哈希表第%d个链表插入头结点%d\n",s,ht.elem[s]->data);
			}
			else{						//尾插
				ListNode* current=(ht.elem[s]);
				while(current->next!=NULL)
					current=current->next;
				current->next=(ListNode*) malloc(sizeof(ListNode));
				current->next->data=e;
				current->next->next=NULL;
				printf("存在冲突,链式哈希表第%d个链表插入尾节点%d\n",s,current->next->data);
				ht.count++;
			}
		}
	}
	return 1;
}
				
				
int DeleteHash(int key){
	int s=CalHash(key);
	int i=SearchHash(key,&s);
	if(i){
		ListNode* current=ht.elem[s];
		ListNode* prev=NULL;
		if(current->data==key){		//头结点删除
			ht.elem[s]=current->next;
			current->next=NULL;
			printf("哈希表第%d个链表删除头结点%d\n",s,current->data);
			free(current);
			ht.count--;
			return 1;
		}
		while(current!=NULL){		//后续节点删除
			if(current->data!=key){
				prev=current;
				current=current->next;
			}
			else
				break;
		}
		prev->next=current->next;
		current->next=NULL;
		printf("哈希表第%d个链表删除中间节点或尾结点%d\n",s,current->data);
		free(current);
		ht.count--;
		return 1;
	}
	else
		printf("你要删除的元素不存在\n");
	return 0;
}

void freeHash(){
	for(int i=0;i<SIZE;i++){
		ListNode* current=ht.elem[i];
		while(current!=NULL){
			ListNode* temp=current;
			current=current->next;
			temp->next=NULL;
			free(temp);
		}
	}
}

postscript

GDB Niubi

Published 19 original articles · won praise 1 · views 3136

Guess you like

Origin blog.csdn.net/qq_41603639/article/details/102596637