具体代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
class
Program
{
static void Main( string [] args)
{
const int HUMAN_MAX = 17 ;
const int HUMAN_INDEX = 3 ;
const int HUMAN_NUMBER = 0 ;
LinkTable < Human > list = new LinkTable < Human > ();
for ( int i = 0 ; i < HUMAN_MAX; i ++ )
{
list.Add( new Human(i));
}
Human human = list.First;
int number = HUMAN_NUMBER;
do
{
if (number == HUMAN_INDEX)
{
number = 0 ;
Console.WriteLine(human.ID);
list.Remove(human);
}
else
{
number ++ ;
human = (Human)human.Next;
}
}
while (human.Next != human);
Console.WriteLine(human.ID);
Console.Read();
}
public class Human : LinkTableNode {
public Human( int id){
this .ID = id;
}
public int ID{ get ; set ;}
}
/// <summary>
/// 圆环链表
/// </summary>
/// <typeparam name="T"></typeparam>
public class LinkTable < T > where T : LinkTableNode
{
public List < T > list = new List < T > ();
public T First
{
get
{
return list.Count > 0 ? list[ 0 ] : default (T);
}
}
public void Add(T t)
{
t.Previous = list.Count == 0 ? t : list[list.Count - 1 ];
t.Next = list.Count == 0 ? t : list[ 0 ];
if (list.Count > 0 )
{
list[ 0 ].Previous = t;
t.Previous.Next = t;
}
list.Add(t);
}
public void Remove(T t)
{
int i = list.IndexOf(t);
if (list.Count == 1 )
{
list.Remove(t);
}
else
{
t.Previous.Next = t.Next;
t.Next.Previous = t.Previous;
}
}
}
/// <summary>
/// 链表节点
/// </summary>
public class LinkTableNode
{
/// <summary>
/// 下一个节点
/// </summary>
public LinkTableNode Next
{
get ;
set ;
}
/// <summary>
/// 上一个节点
/// </summary>
public LinkTableNode Previous
{
get ;
set ;
}
}
}
{
static void Main( string [] args)
{
const int HUMAN_MAX = 17 ;
const int HUMAN_INDEX = 3 ;
const int HUMAN_NUMBER = 0 ;
LinkTable < Human > list = new LinkTable < Human > ();
for ( int i = 0 ; i < HUMAN_MAX; i ++ )
{
list.Add( new Human(i));
}
Human human = list.First;
int number = HUMAN_NUMBER;
do
{
if (number == HUMAN_INDEX)
{
number = 0 ;
Console.WriteLine(human.ID);
list.Remove(human);
}
else
{
number ++ ;
human = (Human)human.Next;
}
}
while (human.Next != human);
Console.WriteLine(human.ID);
Console.Read();
}
public class Human : LinkTableNode {
public Human( int id){
this .ID = id;
}
public int ID{ get ; set ;}
}
/// <summary>
/// 圆环链表
/// </summary>
/// <typeparam name="T"></typeparam>
public class LinkTable < T > where T : LinkTableNode
{
public List < T > list = new List < T > ();
public T First
{
get
{
return list.Count > 0 ? list[ 0 ] : default (T);
}
}
public void Add(T t)
{
t.Previous = list.Count == 0 ? t : list[list.Count - 1 ];
t.Next = list.Count == 0 ? t : list[ 0 ];
if (list.Count > 0 )
{
list[ 0 ].Previous = t;
t.Previous.Next = t;
}
list.Add(t);
}
public void Remove(T t)
{
int i = list.IndexOf(t);
if (list.Count == 1 )
{
list.Remove(t);
}
else
{
t.Previous.Next = t.Next;
t.Next.Previous = t.Previous;
}
}
}
/// <summary>
/// 链表节点
/// </summary>
public class LinkTableNode
{
/// <summary>
/// 下一个节点
/// </summary>
public LinkTableNode Next
{
get ;
set ;
}
/// <summary>
/// 上一个节点
/// </summary>
public LinkTableNode Previous
{
get ;
set ;
}
}
}