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

时间:2021-02-11 07:15:58

I need a circular linked list, so I am wondering if LinkedList is a circular linked list?

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

5 个解决方案

#1


14  

No. It is a doubly linked list, but not a circular linked list. See MSDN for details on this.

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

LinkedList<T> makes a good foundation for your own circular linked list, however. But it does have a definite First and Last property, and will not enumerate around these, which a proper circular linked list will.

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

#2


48  

A quick solution to using it in a circular fashion, whenever you want to move the "next" piece in the list:

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

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

Where current is LinkedListNode<T>.

其中current是LinkedListNode

#3


6  

If you need a circular data structure, have a look at the C5 generic collections library. They have any collection that's imaginably useful in there, including a circular queue (which might help you).

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

#4


4  

No, its not. See MSDN

不,这不对。请参阅MSDN

#5


4  

While the public API of the LinkedList is not circular, internally it actually is. Consulting the reference source, you can see how it's implemented:

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

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

Of course, to hide the fact that it's circular, properties and methods that traverse the list make checks to prevent wrapping back to the head.

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

LinkedListNode:

public LinkedListNode<T> Next {
    get { return next == null || next == list.head? null: next;}
}

public LinkedListNode<T> 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;
}

#1


14  

No. It is a doubly linked list, but not a circular linked list. See MSDN for details on this.

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

LinkedList<T> makes a good foundation for your own circular linked list, however. But it does have a definite First and Last property, and will not enumerate around these, which a proper circular linked list will.

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

#2


48  

A quick solution to using it in a circular fashion, whenever you want to move the "next" piece in the list:

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

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

Where current is LinkedListNode<T>.

其中current是LinkedListNode

#3


6  

If you need a circular data structure, have a look at the C5 generic collections library. They have any collection that's imaginably useful in there, including a circular queue (which might help you).

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

#4


4  

No, its not. See MSDN

不,这不对。请参阅MSDN

#5


4  

While the public API of the LinkedList is not circular, internally it actually is. Consulting the reference source, you can see how it's implemented:

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

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

Of course, to hide the fact that it's circular, properties and methods that traverse the list make checks to prevent wrapping back to the head.

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

LinkedListNode:

public LinkedListNode<T> Next {
    get { return next == null || next == list.head? null: next;}
}

public LinkedListNode<T> 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;
}