C++ 链式哈希程序17 Mar 2025 | 5 分钟阅读 哈希表链表法究竟是什么?链表法是一种哈希表冲突避免技术。 当哈希表中的两个键哈希到同一个索引时,就会发生冲突。冲突是一个问题,因为哈希表中的每个槽位只应该容纳一个元素。 ![]() 链表法链表法中的哈希表是一个链表数组,每个索引都有自己的链表。 所有映射到相同索引的键值对都将存储在该索引的链表中。 链表法的优点
链表法的实现让我们编写一个哈希函数,以确保我们的哈希表有 'N' 个桶。 要将节点添加到哈希表,我们必须首先确定给定键的哈希索引。它也可以使用哈希函数计算。 示例:哈希索引 = 键 % 桶数 插入: 移动到与上面计算的哈希索引对应的桶,并将新节点插入到列表的末尾。 删除: 要从哈希表中删除一个节点,计算键的哈希索引,移动到与计算的哈希索引对应的桶,在当前桶中的列表中搜索具有给定键的节点,并将其删除(如果找到)。 算法对于插入 对于删除 对于搜索 编码输出 1. Insert element into the table 2. Search element from the key 3. Delete element at a key 4. Exit Enter your choice: 1 Enter element to be inserted: 2 Enter key at which element to be inserted: 1 1. Insert element into the table 2. Search element from the key 3. Delete element at a key 4. Exit Enter your choice: 1 Enter element to be inserted: 3 Enter key at which element to be inserted: 4 1. Insert element into the table 2. Search element from the key 3. Delete element at a key 4. Exit Enter your choice: 1 Enter element to be inserted: 7 Enter key at which element to be inserted: 6 1. Insert element into the table 2. Search element from the key 3. Delete element at a key 4. Exit Enter your choice: 1 Enter element to be inserted: 8 Enter key at which element to be inserted: 9 1. Insert element into the table 2. Search element from the key 3. Delete element at a key 4. Exit Enter your choice: 2 Enter key of the element to be searched: 6 Element found at key 6: 7 1. Insert element into the table 2. Search element from the key 3. Delete element at a key 4. Exit Enter your choice: 2 Enter key of the element to be searched: 7 No Element found at key 7 1. Insert element into the table 2. Search element from the key 3. Delete element at a key 4. Exit Enter your choice: 3 Enter key of the element to be deleted: 9 Element Deleted 1. Insert element into the table 2. Search element from the key 3. Delete element at a key 4. ExitC Enter your choice: 4 时间复杂度
下一个主题所有元素都是斐波那契数的最大子集 |
这两种都是我们主要用于竞技编程和其他行业的编程语言,因为它们提供了各种功能。C++ 在程序员中非常流行,因为它非常高效,动态使用内存,并且运行速度快。另一方面,Java,...
7 分钟阅读
C++ 简介是最常用的编程语言之一,主要用于开发高性能应用程序、操作系统和游戏。C++ 是一种功能强大且高效的语言,为复杂的数据处理任务提供了广泛的数据结构和算法……
阅读9分钟
在本文中,您将学习 C++ 中运算符重载的规则。C++ 中运算符重载有几条规则。一些主要规则如下: 1. 语法 通过定义一个函数并后跟 operator 关键字来定义运算符重载...
阅读 3 分钟
“自定义排序字符串”是指一种对字符串进行排序的特定方式,该方式偏离了标准的词典(字典)顺序。在自定义排序中,您为字符串中的字符或子字符串定义自己的顺序。此自定义顺序可以基于各种标准,例如特定的字符...
阅读9分钟
在本文中,您将了解仿函数与函数之间的区别。但在讨论它们的区别之前,您必须了解 C++ 中的仿函数和函数。C++ 中的仿函数是什么?仿函数有时被称为“函数对象”。它是一个对象……
阅读 6 分钟
在本文中,您将了解 C++ 中 basic_istream::peek() 方法的语法、功能和示例。什么是 basic_istream::peek() 方法?在 C++ 中,可以使用 peek() 方法检查输入流中的字符,而无需提取它。它是...
阅读 4 分钟
在 C++ 的世界里,高效的内存管理至关重要,因为构建健壮且高性能的应用程序取决于最佳的资源利用。这项工作的核心在于 std::allocator 类,它是动态内存分配的基础元素。在本文中,我们将踏上一次旅程...
阅读 4 分钟
然而,C++ 编程语言被认为是最高效的语言之一,具有大量的灵活选项和强大的机制。STL 是其众多宝藏之一,拥有各种容器和算法。multimap 是其中之一……
阅读 3 分钟
在 C++ 中。但在讨论区别之前,我们必须了解 `std::swap` 和 `std::vector::swap` 在 C++ 中的作用。`std::swap` 是什么?`std::swap` 工具函数定义在 C++ 标准库的 `
阅读 4 分钟
介绍 一个名为“”的计算工具被组装起来,用于根据用户定义的输入确定中心二十面体数。二十面体是一个具有二十个等边三角形面的多面体,其顶点是这些数字序列的起点。中心二十面体数在数学中很重要……
5 分钟阅读
我们请求您订阅我们的新闻通讯以获取最新更新。
我们提供所有技术(如 Java 教程、Android、Java 框架)的教程和面试问题
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India