双向链表(数据结构)2025年4月21日 | 阅读 10 分钟 双向链表是一种复杂的链表类型,其中每个节点都包含一个指向序列中前一个节点以及后一个节点的指针。因此,在双向链表中,一个节点包含三个部分:节点数据、指向序列中后一个节点的指针(next 指针)、指向前一个节点的指针(previous 指针)。下面图示了一个双向链表的节点示例。 ![]() 下图展示了一个包含三个节点的双向链表,其数据部分从 1 到 3。 ![]() 在 C 语言中,双向链表中节点的结构可以表示为: 第一个节点的 prev 部分和最后一个节点的 next 部分将始终包含 null,表示两个方向的结束。 在单向链表中,我们只能单向遍历,因为每个节点只包含下一个节点的地址,而没有任何关于其前一个节点的信息。然而,双向链表克服了单向链表的这一限制。由于列表的每个节点都包含其前一个节点的地址,我们可以通过使用每个节点中存储的前一个地址来查找前一个节点的所有详细信息。 双向链表的内存表示双向链表的内存表示如下图所示。通常,双向链表为每个节点消耗更多空间,因此会导致插入和删除等基本操作的开销更大。但是,我们可以轻松地操作列表中的元素,因为列表在两个方向(前向和后向)都维护着指针。 在下图所示的列表中,第一个元素 i.e. 13 存储在地址 1。head 指针指向起始地址 1。由于这是添加到列表中的第一个元素,因此列表的 prev 包含 null。列表的下一个节点位于地址 4,因此第一个节点在其 next 指针中包含 4。 我们可以以这种方式遍历列表,直到找到任何 next 部分包含 null 或 -1 的节点。 ![]() 双向链表的操作节点创建 有关双向链表的所有剩余操作将在下表中进行说明。
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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value12 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value123 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 1 Enter Item value1234 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 1234 123 12 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 2 Enter value89 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 3 Enter the location1 Enter value12345 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 1234 123 12345 12 89 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 4 node deleted *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 5 node deleted *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 123 12345 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 6 Enter the data after which the node is to be deleted : 123 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 8 printing values... 123 *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 7 Enter item which you want to search? 123 item found at location 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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 6 Enter the data after which the node is to be deleted : 123 Can't delete *********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 the node after the given data 7.Search 8.Show 9.Exit Enter your choice? 9 Exited.. 关于双向链表的选择题问题 1:连接两个循环双向链表和两个双向链表所需的时间是多少?
答案 选项 C: O(1), O(n) 通过至少一个链表的最后一个节点指针(当然,除非另有说明,否则还需要链表头指针),可以在 O(1) 时间内轻松连接两个循环双向链表。双向链表需要 O(n) 时间,因为我们无法在 O(1) 时间内找到双向链表的最后一个元素。 问题 2:如果所有双向链表的节点数据都相同,则找出以下程序的功能。
答案 d) 以上都不是 ![]() First = 100(设为 head) Current = 100 Second = 200 Free(current → next) = free(200) Current → next = 300 Current → next → prev = 100 ![]() First = 100 Current = 100 Second = NULL Free(current → next) = free(300) Current → next = 300 NULL Current → next → prev = 100 NULL ![]() 退出循环 对于连续相同数据的节点,除了第一个节点外,其余节点都将被删除。 因此,选项 (D) 是正确的。 问题 3:是否可以通过每个节点仅使用一个指针来创建双向链表?
答案 (b) 是的,可以通过存储前一个节点和后一个节点的地址的 XOR 来实现。它们被称为 XOR 链表。 问题 4:在通过单向链表实现双向链表时需要多少个额外的指针?
答案 (a) 1。双向链表需要两个指针,而我们知道单向链表已经有一个指针,所以我们需要一个额外的指针才能从单向链表实现双向链表。 问题 5:双向链表每个节点所需的最小字段数是。
答案 (a) 3,双向链表的每个节点都有三个字段,即数据部分、指向下一个节点的指针、指向前一个节点的指针。 下一主题循环链表 |
我们请求您订阅我们的新闻通讯以获取最新更新。