循环双向链表

2025年4月21日 | 6分钟阅读

循环双向链表是一种更复杂的数据结构,其中一个节点包含指向其前一个节点和下一个节点的指针。循环双向链表中没有任何节点包含 NULL。链表的最后一个节点包含链表第一个节点的地址。链表的第一个节点在其前驱指针中也包含最后一个节点的地址。

下图显示了一个循环双向链表。


Circular Doubly Linked List

由于循环双向链表的结构包含三个部分,因此它在每个节点上需要更多的空间,并且基本操作的开销也更大。但是,循环双向链表提供了方便的指针操作,并且搜索效率提高了一倍。

循环双向链表的内存管理

下图显示了为循环双向链表分配内存的方式。变量 head 包含链表第一个元素的地址,即 1,因此链表的起始节点包含数据 A,存储在地址 1。由于链表的每个节点都应该包含三个部分,因此链表的起始节点包含最后一个节点的地址,即 8,以及下一个节点,即 4。链表中存储在地址 8 并包含数据 6 的最后一个节点,包含链表第一个节点的地址,如图所示,即 1。在循环双向链表中,最后一个节点通过存储在最后一个节点 next 部分的第一个节点的地址来识别,因此包含第一个节点地址的节点实际上是链表的最后一个节点。


Circular Doubly Linked List

循环双向链表上的操作

可以在循环双向链表上执行各种操作。循环双向链表的节点结构与双向链表类似。但是,循环双向链表上的操作在下表中描述。

序号操作描述
1在开头插入在循环双向链表开头添加节点。
2在末尾插入在循环双向链表末尾添加节点。
3在开头删除从循环双向链表开头删除节点。
4在末尾删除从循环双向链表末尾删除节点。

循环双向链表的遍历和搜索与循环单向链表的遍历和搜索类似。

C 语言实现循环双向链表所有操作的程序

示例

编译并运行

输出

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
1

Enter Item value123

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
2

Enter value234

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
1

Enter Item value90

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
2

Enter value80

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
3

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
4

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
6

 printing values ... 
123

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
5

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 Beginning
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search
6.Show
7.Exit

Enter your choice?
7