.NET中的LinkedList是循环链表吗?

我需要一个循环链表,所以我想知道LinkedList是否是循环链表?

不是。它是一个双向链表,但不是循环链表。 有关详细信息,请参阅MSDN 。

然而,LinkedList 为您自己的循环链表奠定了良好的基础。 但它确实有一个明确的First和Last属性,并且不会枚举这些,这是一个合适的循环链表。

无论何时您想要移动列表中的“下一个”部分,都可以快速解决以循环方式使用它的方法:

 current = current.Next ?? current.List.First; 

其中current是LinkedListNode

如果需要循环数据结构,请查看C5generics集合库 。 他们有任何在那里可以想象的有用的集合,包括一个循环队列 (可能对你有帮助)。

不,这不对。 请参阅MSDN

虽然LinkedList的公共API不是循环的,但实际上它是内部的。 咨询参考源 ,您可以看到它是如何实现的:

 // This LinkedList is a doubly-Linked circular list. internal LinkedListNode head; 

当然,为了隐藏它是循环的事实,遍历列表的属性和方法进行检查以防止回绕到头部。

一个LinkedListNode:

 public LinkedListNode Next { get { return next == null || next == list.head? null: next;} } public LinkedListNode Previous { get { return prev == null || this == list.head? null: prev;} } 

LinkedList.Enumerator:

 public bool MoveNext() { if (version != list.version) { throw new InvalidOperationException(SR.GetString(SR.InvalidOperation_EnumFailedVersion)); } if (node == null) { index = list.Count + 1; return false; } ++index; current = node.item; node = node.next; if (node == list.head) { node = null; } return true; }