循环链表可以是单链表,也可以是双链表。链表的尾节点的后继节点指向头结点便成了循环链表。
我们在这里继承双链表实现循环链表,当到达双链表的表尾时,让游标指向第0个节点;当到达双链表的开头时,让游标指向结尾节点,这样就实现了循环双链表。结尾用一个经典的约瑟夫问题来作循环链表的应用示例。
1.循环链表代码:
2.用循环链表解决约瑟夫问题
问题描述:N个人围成圆圈,从1开始报数,到第M个人令其出列,然后下一个人继续从1开始报数,到第M个人令其出列,如此下去,直到只剩一个人为止。显示最后一个人为剩者。
代码:
运行结果:
如果把上面高亮显示的语句改为:list.GetNext();
运行结果变为:
我们在这里继承双链表实现循环链表,当到达双链表的表尾时,让游标指向第0个节点;当到达双链表的开头时,让游标指向结尾节点,这样就实现了循环双链表。结尾用一个经典的约瑟夫问题来作循环链表的应用示例。
1.循环链表代码:
/*
* File : CircularlyLinkedList.cs
* Author : Zhenxing Zhou
* Date : 2008-12-07
* Blog : http://www.xianfen.net/
*/
using System;
namespace Xianfen.Net.DataStructure
{
public class CircularlyLinkedList<T> : DoubleLinkedList<T>
{
private DoubleLinkedListNode<T> m_CurrentNode;
private int m_CurrentIndex;
public int CurrentIndex
{
get { return m_CurrentIndex; }
}
public CircularlyLinkedList()
: base()
{
m_CurrentNode = m_Head.Next;
m_CurrentIndex = 0;
}
public CircularlyLinkedList(T t)
: base(t)
{
m_CurrentNode = m_Head.Next;
m_CurrentIndex = 0;
}
public T GetCurrent()
{
if (m_Count == 0)
{
throw new IndexOutOfRangeException();
}
return m_CurrentNode.Value;
}
public T GetNext()
{
if (m_Count == 0)
{
throw new IndexOutOfRangeException();
}
if (m_CurrentNode != null)
{
m_CurrentNode = m_CurrentNode.Next;
m_CurrentIndex++;
}
if (m_CurrentNode == null)
{
m_CurrentNode = m_Head.Next;
m_CurrentIndex = 0;
}
return m_CurrentNode.Value;
}
public T GetPrevious()
{
if (m_Count == 0)
{
throw new IndexOutOfRangeException();
}
if (m_CurrentNode != null)
{
m_CurrentNode = m_CurrentNode.Prior;
m_CurrentIndex--;
}
if (m_CurrentNode == null || m_CurrentNode == m_Head)
{
m_CurrentNode = m_Tail;
m_CurrentIndex = m_Count - 1;
}
return m_CurrentNode.Value;
}
}
}
* File : CircularlyLinkedList.cs
* Author : Zhenxing Zhou
* Date : 2008-12-07
* Blog : http://www.xianfen.net/
*/
using System;
namespace Xianfen.Net.DataStructure
{
public class CircularlyLinkedList<T> : DoubleLinkedList<T>
{
private DoubleLinkedListNode<T> m_CurrentNode;
private int m_CurrentIndex;
public int CurrentIndex
{
get { return m_CurrentIndex; }
}
public CircularlyLinkedList()
: base()
{
m_CurrentNode = m_Head.Next;
m_CurrentIndex = 0;
}
public CircularlyLinkedList(T t)
: base(t)
{
m_CurrentNode = m_Head.Next;
m_CurrentIndex = 0;
}
public T GetCurrent()
{
if (m_Count == 0)
{
throw new IndexOutOfRangeException();
}
return m_CurrentNode.Value;
}
public T GetNext()
{
if (m_Count == 0)
{
throw new IndexOutOfRangeException();
}
if (m_CurrentNode != null)
{
m_CurrentNode = m_CurrentNode.Next;
m_CurrentIndex++;
}
if (m_CurrentNode == null)
{
m_CurrentNode = m_Head.Next;
m_CurrentIndex = 0;
}
return m_CurrentNode.Value;
}
public T GetPrevious()
{
if (m_Count == 0)
{
throw new IndexOutOfRangeException();
}
if (m_CurrentNode != null)
{
m_CurrentNode = m_CurrentNode.Prior;
m_CurrentIndex--;
}
if (m_CurrentNode == null || m_CurrentNode == m_Head)
{
m_CurrentNode = m_Tail;
m_CurrentIndex = m_Count - 1;
}
return m_CurrentNode.Value;
}
}
}
2.用循环链表解决约瑟夫问题
问题描述:N个人围成圆圈,从1开始报数,到第M个人令其出列,然后下一个人继续从1开始报数,到第M个人令其出列,如此下去,直到只剩一个人为止。显示最后一个人为剩者。
代码:
const int M = 9;
const int N = 7;
CircularlyLinkedList<int> list = new CircularlyLinkedList<int>();
//填充循环链表
for (int i = 1; i < M; i++)
{
list.Add(i);
}
int tempCounter = 0;
while (list.Count > 1)
{
tempCounter++;
list.GetPrevious();
// 选中者出列
if (tempCounter == N)
{
tempCounter = 0;
Console.WriteLine(list.GetCurrent() + " 出列!");
list.RemoveAt(list.CurrentIndex);
}
}
Console.WriteLine(list.GetNext() + " 为剩者");
const int N = 7;
CircularlyLinkedList<int> list = new CircularlyLinkedList<int>();
//填充循环链表
for (int i = 1; i < M; i++)
{
list.Add(i);
}
int tempCounter = 0;
while (list.Count > 1)
{
tempCounter++;
list.GetPrevious();
// 选中者出列
if (tempCounter == N)
{
tempCounter = 0;
Console.WriteLine(list.GetCurrent() + " 出列!");
list.RemoveAt(list.CurrentIndex);
}
}
Console.WriteLine(list.GetNext() + " 为剩者");
运行结果:
2 出列!
3 出列!
1 出列!
7 出列!
4 出列!
8 出列!
6 出列!
5 为剩者
3 出列!
1 出列!
7 出列!
4 出列!
8 出列!
6 出列!
5 为剩者
如果把上面高亮显示的语句改为:list.GetNext();
运行结果变为:
7 出列!
6 出列!
8 出列!
2 出列!
5 出列!
1 出列!
3 出列!
4 为剩者
6 出列!
8 出列!
2 出列!
5 出列!
1 出列!
3 出列!
4 为剩者
分类:
DataStructure
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述