C 语言二次探测程序7 Jan 2025 | 7 分钟阅读 在本文中,我们将讨论 C 语言中的二次探测问题。 问题描述本 C 程序实现了二次探测的哈希表。哈希表是一种使用哈希函数将键映射到值的有序数组。线性探测和二次探测是相似的。区别在于,如果我们尝试将元素插入到一个已经被占用的槽中,我们会检查1^1=1 个元素,2^2=4 个元素,3^2=9 个元素,4^2=16 个元素等等。 问题解决方案
注意 在这种情况下,我们将使用以下公式进行探测,而不是使用线性探测: (currentPosition + h) % arraySize => 线性探测 (currentPosition + (h * h)) % arraySize => 二次探测 其中h = 1, 2, 3, 4,依此类推。
二次探测程序让我们举个例子来理解 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 说明
下一主题C 语言编程测试 |
我们请求您订阅我们的新闻通讯以获取最新更新。