循环链表可以是单链表,也可以是双链表。链表的尾节点的后继节点指向头结点便成了循环链表。
我们在这里继承双链表实现循环链表,当到达双链表的表尾时,让游标指向第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 为剩者