【无标题】

HashTable
Hash函数:除留取余法,冲突解决:链地址法
//除留取余法+链表法
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define ElemType int
#define P 13
typedef struct HashNode
{
	ElemType data;
	struct HashNode *link;
}HashNode;
typedef HashNode*  HashTable[P];

void InitHashTable(HashTable &ht)
{
	for(int i=0;i<P;i++){
		ht[i] = NULL; //初始化数组
	}
}
int Hash(ElemType key)
{
	return key % P; //hash函数。 根据关键字映射到关键字对应的地址
}

void InsertHashTable(HashTable &ht,ElemType x)
{
	int index = Hash(x);
	HashNode *s = (HashNode*)malloc(sizeof(HashNode));
	assert(s != NULL);
	s->data = x;
	//头插法
	s->link = ht[index];
	ht[index] = s;
}
void PrintHashTable(HashTable &ht)
{
	for(int i=0;i<P;i++){
		printf("%d : ",i);
		HashNode *p = ht[i];		
		while(p){
			printf("%d-->",p->data);
			p = p->link;
		}
		printf("Nul.\n");
	}
}
HashNode* SearchHashTable(HashTable &ht,ElemType key)
{
	int index = Hash(key);
	HashNode *p = ht[index];
	while(p!=NULL && p->data!=key)
		p = p->link;
	return p;
}
bool RemoveHashTable(HashTable &ht,ElemType key)
{
	HashNode *p = SearchHashTable(ht,key);
	if(p == NULL)
		return false;
	int index = Hash(key);
	HashNode *q = ht[index];
	if(q == p){ //数组当前位置上就是要删除的元素
		ht[index] = p->link;
		free(p);
		return true;
	}
	while(q->link != p) //数组当前位置上不是要删除的元素,向链表后寻找到相应的元素,进行删除
		q = q->link;
	q->link = p->link;
	free(p);
	return true;

}
///
int main()
{
	HashTable ht;
	InitHashTable(ht);

	ElemType a[] = {19,14,23,1,68,20,84,27,55,11,10,79};
	int n = sizeof(a)/sizeof(ElemType);
	for(int i=0;i<n;i++){
		InsertHashTable(ht,a[i]);
	}
	PrintHashTable(ht);

	ElemType key = 27;
	HashNode *p = SearchHashTable(ht,key);
	
	RemoveHashTable(ht,key);
	PrintHashTable(ht);
	return 0;
}

Hash函数:除留取余法,冲突解决:溢出桶法
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

//溢出桶法
/*溢出桶法的思想是(此处以具体数值举例说明):
  每个桶可以容纳3个数据,如果此位置超过三个数据,
  则新创建一个桶来装数据,依次类推。
*/
#define P 7 		  	   //数组容量
#define NULL_DATA -1 	   //当前位置没有数据时为-1
#define BUCKET_NODE_SIZE 3 //每个桶的容量为3
struct bucket_node
{
	int data[BUCKET_NODE_SIZE];
	struct bucket_node *next;
};
struct bucket_node hash_table[P]; //定义一个hash_table

void Init_bucket_node()
{
	for(int i=0;i<P;i++){
		for(int j=0;j<BUCKET_NODE_SIZE;j++){
			hash_table[i].data[j] = NULL_DATA; //初始化
		}
		hash_table[i].next = NULL;		
	}
}
int Hash(int key)
{
	return key % P; //hash函数,计算位置
}
int Insert_new_element(int x)
{
	int index = Hash(x);
	//1.原来数组中有位置,进行插入
	for(int i=0;i<BUCKET_NODE_SIZE;i++){
		if(hash_table[index].data[i] == NULL_DATA){
			hash_table[index].data[i] = x;
			return 0; //成功返回0
		}
	}
	//2.数组已满,没有位置了,需要溢出桶来装
	//2.1有溢出桶,查看溢出桶是否还有位置
	bucket_node *p = &hash_table[index];
	while(p->next != NULL){
		p = p->next;
		for(int i=0;i<BUCKET_NODE_SIZE;i++){
			if(p->data[i] == NULL_DATA){
				p->data[i] = x;
				return 0;
			}
		}		
	}
	//2.2当前溢出桶也满了,需要创建溢出桶
	bucket_node *s = (bucket_node*)malloc(sizeof(bucket_node));
	assert(s != NULL);
	for(int j=1;j<BUCKET_NODE_SIZE;j++){
		s->data[j] = NULL_DATA;
	}
	s->next = NULL;
	s->data[0] = x; //数据一定是存放在新创建的溢出桶的第一个位置
	p->next = s; //连接
	return 0;
}
void PrintHashTable()
{
	for(int i=0;i<P;i++){
		bucket_node *p = &hash_table[i];
		printf("%d : ",i);
		while(p){
			for(int j=0;j<BUCKET_NODE_SIZE;j++){
				if(p->data[j] != NULL_DATA)
					printf("%d-->",p->data[j]);
			}
			p = p->next;
		}
		printf("\n");
	}
}
int main()
{
	Init_bucket_node();
	int a[] = {1,8,15,22,29,36,43};
	int n = sizeof(a)/sizeof(a[0]);
	for(int i=0;i<n;i++){
		Insert_new_element(a[i]);
	}
	PrintHashTable();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值