C 语言通用链表2024年8月28日 | 14 分钟阅读 链表是一种线性数据集合,其元素的顺序由其在内存中的物理位置决定,而不是由其物理顺序决定。相反,每个元素都指向下一个元素。它是一种由节点组成的数据结构,这些节点代表一个序列。因此,在最基本的形式中,每个节点都包含数据和一个指向序列中下一个节点的引用(链接)。在迭代过程中,这种结构可以有效地从序列中的任何位置插入和删除元素。更复杂的变体包含额外的链接,可以更有效地在任意位置插入和删除节点。 链表的一个缺点是访问时间是线性的,并且难以流水线化。无法提供更快的访问,例如随机访问。与链表相比,数组具有更好的缓存局部性。 链表是最基本且广泛使用的数据结构之一。它们可用于实现各种其他流行的抽象数据类型,例如列表、堆栈、队列、关联数组和 S 表达式;但是,直接实现这些数据结构而不使用链表作为基础或底层数据结构也并不罕见。 与传统数组相比,链表的主要优点是可以在不重新分配或重组整个结构的情况下轻松插入或删除列表元素,因为数据项不需要在内存或磁盘上连续存储,而运行时重组数组是一个成本更高的操作。通过在列表遍历过程中保留被添加或删除链接之前的链接,链表可以在常数次操作中在列表的任何位置添加和删除节点。然而,由于简单的链表不支持数据随机访问或高效索引,许多基本操作,例如获取列表的最后一个节点、定位包含给定数据的节点或定位应插入新节点的the位置,可能需要遍历大部分或所有列表元素。使用链表的优点和缺点列在下面。 动态数组是一种数据结构,它在内存中以连续方式分配所有元素,并跟踪当前元素的数量。如果动态数组的分配空间超出,它将被重新分配并(可能)复制,这是一个昂贵的操作。与动态数组相比,使用链表有几个优点。在列表的特定位置插入或删除元素是一个恒定时间操作(除非我们已经为要删除的节点之前或插入点之前的节点建立了指针)。相比之下,在动态数组的随机位置插入平均需要移动一半的元素,在最坏的情况下需要移动所有元素。虽然可以通过将数组槽标记为“空闲”来在恒定时间内“删除”数组中的元素,但这会导致碎片化,从而影响迭代性能。 根据指针的类型,链表的节点具有链表节点指针的方向。 链表类型链表有不同的类型,其中一些主要的链表类型是:
单向链表在单向链表中,单向链表的节点指向存储下一个节点的内存位置的地址。同样,以这种方式,所有节点都存储各自下一个节点的地址。除了最后一个节点,最后一个节点的指针中的地址为 NULL,表示它是链表的最后一个节点。 代码 现在让我们看一个示例代码,用于在 C 编程语言中创建单向链表,向列表中添加元素,然后遍历单向链表。 输出 上面的代码产生以下输出: Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 1 Enter Element for Insert Linked list: 2 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 1 Enter Element for Insert Linked list: 3 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 1 Enter Element for Insert Linked list: 6 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 1 Enter Element for Insert Linked list: 89 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 1 Enter Element for Insert Linked list: 56 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 1 Enter Element for Insert Linked list: 31 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 1 Enter Element for Insert Linked list: 78 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 3 Display Linked list: # 2 # # 3 # # 6 # # 89 # # 56 # # 31 # # 78 # No Of Items In Linked list: 7 Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 2 No of Items In Linked list: 7 Display Linked list: Enter Position for Delete Element: 4 Deleted Successfully Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 3 Display Linked list: # 2 # # 3 # # 6 # # 56 # # 31 # # 78 # No Of Items In Linked list: 6 Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Singly Linked list. 2. To Delete Data from the Singly Linked list. 3. To Display Data present in the Singly Linked list. 4. To Display the count of nodes in the Singly Linked list. 4 No of Items in Linked list: 6 Type [N or n] to terminate the program. Type [Y or y] to continue the program. n 在上面的代码中,我们为链表的各种功能创建了不同的函数,例如向链表中插入新节点,显示链表中所有节点的数据,删除链表中特定位置的特定节点,以及计算链表中节点的总数。 在向单向链表中插入新节点时,会检查第一个节点是否为零,
|
我们请求您订阅我们的新闻通讯以获取最新更新。