数据结构中的单向链表2025 年 4 月 21 日 | 阅读 17 分钟 链表乍一看可能有点复杂,但它们非常实用。本质上,你有一堆独立的节点,就像单独的积木。每个节点存储一块数据,比如一个数字或一些有用的信息。但不仅如此——它还有一个特殊的指针,指向序列中的下一个节点。 现在,与数组中所有元素都整齐地排列在内存中不同,这些节点可以散布在内存的各个位置。指针将它们连接成一个链。第一个节点称为头节点,最后一个节点指向 null,表示列表的末尾。 一个主要的优点是,你不需要预先决定列表需要多少内存。它可以通过在任何可用空间添加新节点来有机地增长。无需像数组那样移动所有内容或重新分配更大的内存块。 另一个优点是插入或删除项目非常快捷方便。你所做的只是更新一些指针来链接或取消链接节点。不需要大量的数据洗牌。这使得链表在你频繁添加或删除内容时非常方便。 当然,也有一些缺点。访问随机元素不像数组那样直接,因为你必须从头开始沿着链条查找。但对于主要处理开头或结尾的情况,链表非常棒。
![]() 链表在现实世界中的应用由于其动态特性和在某些场景中的效率,链表在现实世界中具有广泛的应用。让我们探讨其中一些实际用途
这些只是突出链表在现实世界场景中多功能性的一些例子。它们的动态特性、高效的插入和删除操作以及根据需要增长或缩小的能力使它们成为许多领域中宝贵的工具。 为什么使用链表而不是数组?我们一直在使用数组来组织单独存储在内存中的分组元素。但是数组,尽管它们很棒,也有其自身的局限性,这使我们在某些情况下重新考虑我们的选择。了解链表何时可以介入并挽救局面是很好的! 我们需要注意的数组局限性
链表的优点
链表的缺点
虽然链表在动态大小、高效插入/删除和多功能性方面具有优势,但它们也伴随着随机访问性能、额外内存开销以及某些操作中潜在复杂性方面的权衡。选择使用链表还是数组最终取决于应用程序的具体要求,例如数据集的预期大小、插入和删除的频率以及随机访问性能的重要性。 单向链表或单向链单向链表可以定义为有序元素集合。元素的数量可以根据程序的需要而变化。单向链表中的一个节点由两部分组成:数据部分和链接部分。节点的数据部分存储要由节点表示的实际信息,而节点的链接部分存储其直接后继的地址。 单向链或单向链表只能沿一个方向遍历。换句话说,我们可以说每个节点只包含下一个指针,因此我们不能反向遍历列表。 考虑一个例子,其中学生在三个科目中获得的分数存储在链表中,如图所示。 ![]() 在上图中,箭头表示链接。每个节点的数据部分包含学生在不同科目中获得的分数。列表中的最后一个节点由最后一个节点地址部分中存在的空指针标识。我们可以根据需要,在列表的数据部分中拥有任意数量的元素。 复杂度
单向链表上的操作可以在单向链表上执行各种操作。以下是所有此类操作的列表。 节点创建插入单向链表的插入可以在不同的位置执行。根据新节点插入的位置,插入分为以下几类。
删除和遍历从单向链表中删除节点可以在不同的位置执行。根据要删除节点的位置,操作分为以下几类。
C 语言中的链表:菜单驱动程序示例编译并运行输出 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 1 Enter value 1 Node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 2 Enter value? 2 Node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 3 Enter element value1 Enter the location after which you want to insert 1 Node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 8 printing values . . . . . 1 2 1 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 2 Enter value? 123 Node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 1 Enter value 1234 Node inserted *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 4 Node deleted from the begining ... *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 5 Deleted Node from the last ... *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 6 Enter the location of the node after which you want to perform deletion 1 Deleted node 2 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 8 printing values . . . . . 1 1 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 7 Enter item which you want to search? 1 item found at location 1 item found at location 2 *********Main Menu********* Choose one option from the following list ... =============================================== 1.Insert in begining 2.Insert at last 3.Insert at any random location 4.Delete from Beginning 5.Delete from last 6.Delete node after specified location 7.Search for an element 8.Show 9.Exit Enter your choice? 9 Python 程序示例立即执行输出 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 1 Enter the data for the node to push: 12 Node inserted! Next? 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 1 Enter the data for the node to push: 34 Node inserted! Next? 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 2 Enter the data for the node to append: 45 Node Appeneded 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 3 Enter the node data to insert after: 12 Enter the data for the new node: 99 Node inserted 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 7 Data in the list: 34 12 99 45 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 4 Head removed! 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 5 Tail Node Is Updated 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 7 Data in the list: 12 99 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 6 Enter the node data to delete: 99 Node Deleted Succesfully. 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 8 Enter the data to search: 12 There it is! Your data is at node 0 1. Push 2. Append 3. Insert After 4. Delete Head 5. Delete Tail 6. Delete By Value 7. Traverse 8. Search 0. Exit Your move: 0 PS C:\Users\HP> 下一个主题双向链表 |
我们请求您订阅我们的新闻通讯以获取最新更新。