C 语言二次探测程序

7 Jan 2025 | 7 分钟阅读

在本文中,我们将讨论 C 语言中的二次探测问题

问题描述

本 C 程序实现了二次探测哈希表哈希表是一种使用哈希函数将键映射到值的有序数组。线性探测和二次探测是相似的。区别在于,如果我们尝试将元素插入到一个已经被占用的槽中,我们会检查1^1=1 个元素,2^2=4 个元素,3^2=9 个元素,4^2=16 个元素等等。

问题解决方案

  1. 创建一个哈希表或结构数组。
  2. 输入一个将存储在哈希表中的键和值。
  3. 将生成一个与键对应的索引;每个键都存储在一个特定的数组索引中。
  4. 使用生成的索引访问该数组索引中包含的数据。
  5. 如果没有数据,则创建一个,添加数据项(键和值),并增加哈希表的大小。
  6. 如果数据已存在,则在接下来的项中查找一个空位(必要时循环回)来插入新的数据项。

注意

在这种情况下,我们将使用以下公式进行探测,而不是使用线性探测:

(currentPosition + h) % arraySize => 线性探测

(currentPosition + (h * h)) % arraySize => 二次探测

其中h = 1, 2, 3, 4,依此类推。

  1. 使用for 循环访问每个索引处的元素,以显示哈希表的所有元素
  2. 要从哈希表中删除,我们必须首先确定它的索引,然后如果键匹配则删除它。如果不匹配,我们必须搜索元素,直到找到或一个没有输入数据的空区域,这表示哈希表中没有数据。
  3. 退出。

二次探测程序

让我们举个例子来理解 C 语言中二次探测的用法。

输出

运行时测试用例

1. Inserting and Displaying Elements
Implementation of Hash Table in C with Quadratic Probing.

MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 5 25

Key (5) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 15 225

Key (15) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 4
Array[0] has no elements
Array[1] has no elements
Array[2] has no elements
Array[3] has no elements
Array[4] has no elements
Array[5] has elements
5 (key) and 25 (value)
Array[6] has no elements
Array[7] has no elements
Array[8] has no elements
Array[9] has elements
15 (key) and 225 (value)

Do you want to continue? (Press 1 for yes): 0
2. removing an element
Implementation of Hash Table in C with Quadratic Probing.

MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 5 25

Key (5) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 2
Deleting in Hash table
Enter the key to delete: 5

Key (5) has been removed

Do you want to continue? (Press 1 for yes): 0

3. Checking Hash Table Size
Implementation of Hash Table in C with Quadratic Probing.

MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 5 25

Key (5) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1.Insert item in the Hash table
2.Remove item from the Hash table
3.Check the size of Hash table
4.Display Hash table
5.Exit

Please enter your choice-: 1
Inserting element in Hash table
Enter key and value: 15 225

Key (15) has been inserted

Do you want to continue? (Press 1 for yes): 1
MENU-:
1. Insert item in the Hash table
2. Remove item from the Hash table
3. Check the size of Hash table
4. Display Hash table
5. Exit

Please enter your choice-: 3
Size of Hash table is: 2

Do you want to continue? (Press 1 for yes): 0

说明

  1. 在此示例中,程序定义了两个结构:struct hashtable_item 用于表示哈希表中的对象struct item 用于存储键值对Array,哈希表指针,size 用于跟踪条目数量,以及 max 用于哈希表大小,都是全局变量的示例。
  2. hashcode 函数通过对键执行模运算来确定哈希索引。此索引决定了项在哈希表数组中的位置。
  3. init_array 函数初始化了哈希表数组。所有标志都设置为0(表示空槽),数据指针设置为NULL
  4. insert 操作增加了哈希表的库存。哈希函数用于确定索引,二次探测(按 h 的平方值递增)用于查找开放槽。如果槽是空的,则插入一个新项。如果键已存在,则更新该值。如果哈希表已满,则显示适当的消息。
  5. remove_element 函数使用该函数从哈希表中删除内容。它使用哈希函数来确定索引,并使用二次探测来确定键的位置。如果找到键,则通过更改size、更改flag和释放内存来删除对象。如果表已满或键不存在,则显示适当的通知。
  6. display 函数通过遍历哈希表数组来检查每个槽是否有元素,然后进行打印。如果存在元素,则打印其键和值。
  7. size_of_hashtable 函数返回哈希表的当前大小(当前存储的元素数量)。
  8. 主函数使用init_array 初始化哈希表。用户会看到一个包含添加和删除项、检查大小、查看哈希表退出选项的菜单。根据用户的选择,在运行相关函数后,用户可以选择继续或退出。内存为项分配空间,并在用户退出后释放数组。