循环链表

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

在循环单链表中,链表的最后一个节点包含一个指向链表第一个节点的指针。我们可以有循环单链表,也可以有循环双链表。

我们遍历循环单链表,直到到达我们开始的同一个节点。循环单链表没有开始也没有结束。任何节点的下一个部分都没有空值。

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


Circular Singly Linked List

循环链表主要用于操作系统中的任务维护。在计算机科学中有许多循环链表的使用示例,包括浏览器浏览,其中用户过去访问的页面记录以循环链表的形式维护,并且可以通过单击“上一个”按钮再次访问。

循环链表的内存表示

在下面的图像中,循环链表的内存表示包含了一个学生在4门学科中的分数。然而,这张图片只是展示了循环链表在内存中存储的概览。链表的开始或头部指向索引为1的元素,数据部分包含13,下一个部分包含4。这意味着它连接到链表中存储在索引4的节点。

然而,由于我们考虑的是内存中的循环链表,因此链表的最后一个节点包含指向链表第一个节点的地址。


Circular Singly Linked List

我们也可以在内存中有多个链表,具有指向列表中不同开始节点的不同开始指针。最后一个节点由其下一个部分标识,该部分包含列表开始节点的地址。我们必须能够识别任何链表的最后一个节点,以便在遍历列表时找出需要执行的迭代次数。

循环单链表上的操作

插入

序号操作描述
1在开头插入在循环单链表的开头添加一个节点。
2在末尾插入在循环单链表的末尾添加一个节点。

删除与遍历

序号操作描述
1在开头删除从循环单链表的开头删除节点。
2在末尾删除从循环单链表的末尾删除节点。
3搜索比较节点中的每个元素与给定项,并返回项在列表中出现的位置,否则返回null。
4遍历至少访问列表中的每个元素一次,以执行某些特定操作。

用C语言实现的菜单驱动程序,用于实现所有操作

在循环单链表上

示例

编译并运行

输出

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

Choose one option from the following list ...

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

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

Enter your choice?
1

Enter the node data?10

node inserted

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

Choose one option from the following list ...

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

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

Enter your choice?
2

Enter Data?20

node inserted

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

Choose one option from the following list ...

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

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

Enter your choice?
2

Enter Data?30

node inserted

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

Choose one option from the following list ...

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

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

Enter your choice?
3

node deleted

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

Choose one option from the following list ...

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

1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.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.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?
5

Enter item which you want to search?
20
item found at location 1
*********Main Menu*********

Choose one option from the following list ...

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

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

Enter your choice?
6

 printing values ... 
20

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

Choose one option from the following list ...

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

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

Enter your choice?
7

下一主题循环双链表