冲突处理方法----分离链接法

1 前言

常用处理冲突的思路:

  • 换个位置: 开放地址法
  • 同一位置的冲突对象组织在一起:链地址法

2 分离链接法

分离链接法:将相应位置上冲突的所有关键词存储在同一个单链表中

举例说明最直接:设关键字序列为 { 47, 7, 29, 11, 16, 92, 22, 8, 3, 50, 37, 89, 94, 21 };
散列函数取为:h(key) = key mod 11;
用分离链接法处理冲突。
在这里插入图片描述

  • 表中有9个结点只需1次查找,
  • 5个结点需要2次查找,

查找成功的平均查找次数:ASLs=(9+5*2)/ 14 ≈ 1.36

typedef struct ListNode *Position, *List;
struct ListNode {
	ElementType Element;
	Position Next;
};

typedef struct HashTbl {
	int TableSize;
	List TheLists;
}*HashTable;

Position Find(ElementType Key, HashTable H)
{
	Position P;
	int Pos;
	Pos = Hash(Key, H->TableSize);	//初始散列位置
	P = H->TheLists[Pos].Next;		//获得链表头
	while (P != NULL && strcmp(P->Element, Key))
		P = P->Next;
	return P;//P可能是Key的位置,也可能是NULL(元素不存在)
}

总结

  • 分离链接法的思想

猜你喜欢

转载自blog.csdn.net/happyjacob/article/details/84921486