本文继续 《关于某道C#上机题的OO - 策略模式》 中的题目,但这是使用的是双向循环链表。当第一次看到这题我首先想到的是循环链表,但题目要求面向对象的方法,汗~
首先是双向链表的节点类
1 ///
2 /// 双向链表节点
3 ///
4 ///
5 public class DoubleLinkNode
6 {
7 public DoubleLinkNode() { }
8 public DoubleLinkNode(T item)
9 {
10 Value = item;
11 }
12 ///
13 /// 节点值
14 ///
15 public T Value { get; set; }
16 ///
17 /// 下一个节点
18 ///
19 public DoubleLinkNode Next { get; set; }
20 ///
21 /// 前一个节点
22 ///
23 public DoubleLinkNode Previous { get; set; }
24 public override string ToString()
25 {
26 return Value.ToString();
27 }
28 }这里使用的是泛型类,他的优势就不讨论了,下面是循环链表类
///
/// 双向循环链表
///
///
public class CircleList : IEnumerable
{
private int currentIndex;
private DoubleLinkNode current;
///
/// 头节点
///
public DoubleLinkNode First { get; private set; }
///
/// 尾节点
///
public DoubleLinkNode Last { get; private set; }
///
/// 节点数
///
public int Count { get; private set; }
///
/// 索引
///
///
///
public DoubleLinkNode this[int index]
{
get
{
if (Count - index 0)
{
if (currentIndex == index) break;
currentIndex--;
current = current.Previous;
}
}
else
{
Reset();
while (currentIndex o.Value.Equals(item));
if (node == null) return;
Remove(node);
Count--;
}
///
/// 查找节点
///
///
///
public DoubleLinkNode Find(Predicate match)
{
Reset();
while (currentIndex |