这在以下应用程序中对我来说很好:
class Program
{
static void Main(string[] args)
{
LinkedListNode<object> node = new LinkedListNode<object>("Hello World");
node.NextOrFirst();
}
}
static class CircularLinkedList
{
public static LinkedListNode<object> NextOrFirst(this LinkedListNode<object> current)
{
if (current.Next == null)
return current.List.First;
return current.Next;
}
public static LinkedListNode<object> PreviousOrLast(this LinkedListNode<object> current)
{
if (current.Previous == null)
return current.List.Last;
return current.Previous;
}
}
确保您使用正确的泛型类型实例化您的 LinkedListNode,因为使用任何其他类型(即字符串)将不起作用。但是,您可以通过将代码更改为以下内容来扩展它以支持任何类型的 LinkedListNode:
class Program
{
static void Main(string[] args)
{
LinkedListNode<string> node = new LinkedListNode<string>("Hello World");
node.NextOrFirst<string>();
LinkedListNode<int> node2 = new LinkedListNode<int>(3);
node2.NextOrFirst<int>();
}
}
static class CircularLinkedList
{
public static LinkedListNode<T> NextOrFirst<T>(this LinkedListNode<T> current)
{
if (current.Next == null)
return current.List.First;
return current.Next;
}
public static LinkedListNode<T> PreviousOrLast<T>(this LinkedListNode<T> current)
{
if (current.Previous == null)
return current.List.Last;
return current.Previous;
}
}