LinkedList<T> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个双重链接的列表。
generic <typename T>
public ref class LinkedList : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::ICollection
generic <typename T>
public ref class LinkedList : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::ICollection, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename T>
public ref class LinkedList : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::ICollection, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
generic <typename T>
public ref class LinkedList : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::ICollection
public class LinkedList<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection
public class LinkedList<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class LinkedList<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class LinkedList<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
public class LinkedList<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
type LinkedList<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ICollection
type LinkedList<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ICollection
interface IDeserializationCallback
interface ISerializable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type LinkedList<'T> = class
interface ICollection<'T>
interface seq<'T>
interface ICollection
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type LinkedList<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<'T>
interface ISerializable
interface IDeserializationCallback
type LinkedList<'T> = class
interface ICollection<'T>
interface seq<'T>
interface ICollection
interface IEnumerable
Public Class LinkedList(Of T)
Implements ICollection, ICollection(Of T), IEnumerable(Of T), IReadOnlyCollection(Of T)
Public Class LinkedList(Of T)
Implements ICollection, ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), IReadOnlyCollection(Of T), ISerializable
Public Class LinkedList(Of T)
Implements ICollection, ICollection(Of T), IDeserializationCallback, IEnumerable(Of T), ISerializable
Public Class LinkedList(Of T)
Implements ICollection, ICollection(Of T), IEnumerable(Of T)
类型参数
- T
指定链接列表的元素类型。
- 继承
-
LinkedList<T>
- 属性
- 实现
示例
下面的代码示例演示 LinkedList<T> 类的许多功能。
#using <System.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
public ref class Example
{
public:
static void Main()
{
// Create the link list.
array<String^>^ words =
{ "the", "fox", "jumped", "over", "the", "dog" };
LinkedList<String^>^ sentence = gcnew LinkedList<String^>(words);
Display(sentence, "The linked list values:");
Console::WriteLine("sentence.Contains(\"jumped\") = {0}",
sentence->Contains("jumped"));
// Add the word 'today' to the beginning of the linked list.
sentence->AddFirst("today");
Display(sentence, "Test 1: Add 'today' to beginning of the list:");
// Move the first node to be the last node.
LinkedListNode<String^>^ mark1 = sentence->First;
sentence->RemoveFirst();
sentence->AddLast(mark1);
Display(sentence, "Test 2: Move first node to be last node:");
// Change the last node to 'yesterday'.
sentence->RemoveLast();
sentence->AddLast("yesterday");
Display(sentence, "Test 3: Change the last node to 'yesterday':");
// Move the last node to be the first node.
mark1 = sentence->Last;
sentence->RemoveLast();
sentence->AddFirst(mark1);
Display(sentence, "Test 4: Move last node to be first node:");
// Indicate the last occurence of 'the'.
sentence->RemoveFirst();
LinkedListNode<String^>^ current = sentence->FindLast("the");
IndicateNode(current, "Test 5: Indicate last occurence of 'the':");
// Add 'lazy' and 'old' after 'the' (the LinkedListNode named current).
sentence->AddAfter(current, "old");
sentence->AddAfter(current, "lazy");
IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':");
// Indicate 'fox' node.
current = sentence->Find("fox");
IndicateNode(current, "Test 7: Indicate the 'fox' node:");
// Add 'quick' and 'brown' before 'fox':
sentence->AddBefore(current, "quick");
sentence->AddBefore(current, "brown");
IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':");
// Keep a reference to the current node, 'fox',
// and to the previous node in the list. Indicate the 'dog' node.
mark1 = current;
LinkedListNode<String^>^ mark2 = current->Previous;
current = sentence->Find("dog");
IndicateNode(current, "Test 9: Indicate the 'dog' node:");
// The AddBefore method throws an InvalidOperationException
// if you try to add a node that already belongs to a list.
Console::WriteLine("Test 10: Throw exception by adding node (fox) already in the list:");
try
{
sentence->AddBefore(current, mark1);
}
catch (InvalidOperationException^ ex)
{
Console::WriteLine("Exception message: {0}", ex->Message);
}
Console::WriteLine();
// Remove the node referred to by mark1, and then add it
// before the node referred to by current.
// Indicate the node referred to by current.
sentence->Remove(mark1);
sentence->AddBefore(current, mark1);
IndicateNode(current, "Test 11: Move a referenced node (fox) before the current node (dog):");
// Remove the node referred to by current.
sentence->Remove(current);
IndicateNode(current, "Test 12: Remove current node (dog) and attempt to indicate it:");
// Add the node after the node referred to by mark2.
sentence->AddAfter(mark2, current);
IndicateNode(current, "Test 13: Add node removed in test 11 after a referenced node (brown):");
// The Remove method finds and removes the
// first node that that has the specified value.
sentence->Remove("old");
Display(sentence, "Test 14: Remove node that has the value 'old':");
// When the linked list is cast to ICollection(Of String),
// the Add method adds a node to the end of the list.
sentence->RemoveLast();
ICollection<String^>^ icoll = sentence;
icoll->Add("rhinoceros");
Display(sentence, "Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':");
Console::WriteLine("Test 16: Copy the list to an array:");
// Create an array with the same number of
// elements as the inked list.
array<String^>^ sArray = gcnew array<String^>(sentence->Count);
sentence->CopyTo(sArray, 0);
for each (String^ s in sArray)
{
Console::WriteLine(s);
}
// Release all the nodes.
sentence->Clear();
Console::WriteLine();
Console::WriteLine("Test 17: Clear linked list. Contains 'jumped' = {0}",
sentence->Contains("jumped"));
Console::ReadLine();
}
private:
static void Display(LinkedList<String^>^ words, String^ test)
{
Console::WriteLine(test);
for each (String^ word in words)
{
Console::Write(word + " ");
}
Console::WriteLine();
Console::WriteLine();
}
static void IndicateNode(LinkedListNode<String^>^ node, String^ test)
{
Console::WriteLine(test);
if (node->List == nullptr)
{
Console::WriteLine("Node '{0}' is not in the list.\n",
node->Value);
return;
}
StringBuilder^ result = gcnew StringBuilder("(" + node->Value + ")");
LinkedListNode<String^>^ nodeP = node->Previous;
while (nodeP != nullptr)
{
result->Insert(0, nodeP->Value + " ");
nodeP = nodeP->Previous;
}
node = node->Next;
while (node != nullptr)
{
result->Append(" " + node->Value);
node = node->Next;
}
Console::WriteLine(result);
Console::WriteLine();
}
};
int main()
{
Example::Main();
}
//This code example produces the following output:
//
//The linked list values:
//the fox jumped over the dog
//Test 1: Add 'today' to beginning of the list:
//today the fox jumped over the dog
//Test 2: Move first node to be last node:
//the fox jumped over the dog today
//Test 3: Change the last node to 'yesterday':
//the fox jumped over the dog yesterday
//Test 4: Move last node to be first node:
//yesterday the fox jumped over the dog
//Test 5: Indicate last occurence of 'the':
//the fox jumped over (the) dog
//Test 6: Add 'lazy' and 'old' after 'the':
//the fox jumped over (the) lazy old dog
//Test 7: Indicate the 'fox' node:
//the (fox) jumped over the lazy old dog
//Test 8: Add 'quick' and 'brown' before 'fox':
//the quick brown (fox) jumped over the lazy old dog
//Test 9: Indicate the 'dog' node:
//the quick brown fox jumped over the lazy old (dog)
//Test 10: Throw exception by adding node (fox) already in the list:
//Exception message: The LinkedList node belongs a LinkedList.
//Test 11: Move a referenced node (fox) before the current node (dog):
//the quick brown jumped over the lazy old fox (dog)
//Test 12: Remove current node (dog) and attempt to indicate it:
//Node 'dog' is not in the list.
//Test 13: Add node removed in test 11 after a referenced node (brown):
//the quick brown (dog) jumped over the lazy old fox
//Test 14: Remove node that has the value 'old':
//the quick brown dog jumped over the lazy fox
//Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':
//the quick brown dog jumped over the lazy rhinoceros
//Test 16: Copy the list to an array:
//the
//quick
//brown
//dog
//jumped
//over
//the
//lazy
//rhinoceros
//Test 17: Clear linked list. Contains 'jumped' = False
//
using System;
using System.Text;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create the link list.
string[] words =
{ "the", "fox", "jumps", "over", "the", "dog" };
LinkedList<string> sentence = new LinkedList<string>(words);
Display(sentence, "The linked list values:");
// Add the word 'today' to the beginning of the linked list.
sentence.AddFirst("today");
Display(sentence, "Test 1: Add 'today' to beginning of the list:");
// Move the first node to be the last node.
LinkedListNode<string> mark1 = sentence.First;
sentence.RemoveFirst();
sentence.AddLast(mark1);
Display(sentence, "Test 2: Move first node to be last node:");
// Change the last node to 'yesterday'.
sentence.RemoveLast();
sentence.AddLast("yesterday");
Display(sentence, "Test 3: Change the last node to 'yesterday':");
// Move the last node to be the first node.
mark1 = sentence.Last;
sentence.RemoveLast();
sentence.AddFirst(mark1);
Display(sentence, "Test 4: Move last node to be first node:");
// Indicate the last occurence of 'the'.
sentence.RemoveFirst();
LinkedListNode<string> current = sentence.FindLast("the");
IndicateNode(current, "Test 5: Indicate last occurence of 'the':");
// Add 'lazy' and 'old' after 'the' (the LinkedListNode named current).
sentence.AddAfter(current, "old");
sentence.AddAfter(current, "lazy");
IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':");
// Indicate 'fox' node.
current = sentence.Find("fox");
IndicateNode(current, "Test 7: Indicate the 'fox' node:");
// Add 'quick' and 'brown' before 'fox':
sentence.AddBefore(current, "quick");
sentence.AddBefore(current, "brown");
IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':");
// Keep a reference to the current node, 'fox',
// and to the previous node in the list. Indicate the 'dog' node.
mark1 = current;
LinkedListNode<string> mark2 = current.Previous;
current = sentence.Find("dog");
IndicateNode(current, "Test 9: Indicate the 'dog' node:");
// The AddBefore method throws an InvalidOperationException
// if you try to add a node that already belongs to a list.
Console.WriteLine("Test 10: Throw exception by adding node (fox) already in the list:");
try
{
sentence.AddBefore(current, mark1);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Exception message: {0}", ex.Message);
}
Console.WriteLine();
// Remove the node referred to by mark1, and then add it
// before the node referred to by current.
// Indicate the node referred to by current.
sentence.Remove(mark1);
sentence.AddBefore(current, mark1);
IndicateNode(current, "Test 11: Move a referenced node (fox) before the current node (dog):");
// Remove the node referred to by current.
sentence.Remove(current);
IndicateNode(current, "Test 12: Remove current node (dog) and attempt to indicate it:");
// Add the node after the node referred to by mark2.
sentence.AddAfter(mark2, current);
IndicateNode(current, "Test 13: Add node removed in test 11 after a referenced node (brown):");
// The Remove method finds and removes the
// first node that that has the specified value.
sentence.Remove("old");
Display(sentence, "Test 14: Remove node that has the value 'old':");
// When the linked list is cast to ICollection(Of String),
// the Add method adds a node to the end of the list.
sentence.RemoveLast();
ICollection<string> icoll = sentence;
icoll.Add("rhinoceros");
Display(sentence, "Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':");
Console.WriteLine("Test 16: Copy the list to an array:");
// Create an array with the same number of
// elements as the linked list.
string[] sArray = new string[sentence.Count];
sentence.CopyTo(sArray, 0);
foreach (string s in sArray)
{
Console.WriteLine(s);
}
Console.WriteLine("Test 17: linked list Contains 'jumps' = {0}",
sentence.Contains("jumps"));
// Release all the nodes.
sentence.Clear();
Console.WriteLine();
Console.WriteLine("Test 18: Cleared linked list Contains 'jumps' = {0}",
sentence.Contains("jumps"));
Console.ReadLine();
}
private static void Display(LinkedList<string> words, string test)
{
Console.WriteLine(test);
foreach (string word in words)
{
Console.Write(word + " ");
}
Console.WriteLine();
Console.WriteLine();
}
private static void IndicateNode(LinkedListNode<string> node, string test)
{
Console.WriteLine(test);
if (node.List == null)
{
Console.WriteLine("Node '{0}' is not in the list.\n",
node.Value);
return;
}
StringBuilder result = new StringBuilder("(" + node.Value + ")");
LinkedListNode<string> nodeP = node.Previous;
while (nodeP != null)
{
result.Insert(0, nodeP.Value + " ");
nodeP = nodeP.Previous;
}
node = node.Next;
while (node != null)
{
result.Append(" " + node.Value);
node = node.Next;
}
Console.WriteLine(result);
Console.WriteLine();
}
}
//This code example produces the following output:
//
//The linked list values:
//the fox jumps over the dog
//Test 1: Add 'today' to beginning of the list:
//today the fox jumps over the dog
//Test 2: Move first node to be last node:
//the fox jumps over the dog today
//Test 3: Change the last node to 'yesterday':
//the fox jumps over the dog yesterday
//Test 4: Move last node to be first node:
//yesterday the fox jumps over the dog
//Test 5: Indicate last occurence of 'the':
//the fox jumps over (the) dog
//Test 6: Add 'lazy' and 'old' after 'the':
//the fox jumps over (the) lazy old dog
//Test 7: Indicate the 'fox' node:
//the (fox) jumps over the lazy old dog
//Test 8: Add 'quick' and 'brown' before 'fox':
//the quick brown (fox) jumps over the lazy old dog
//Test 9: Indicate the 'dog' node:
//the quick brown fox jumps over the lazy old (dog)
//Test 10: Throw exception by adding node (fox) already in the list:
//Exception message: The LinkedList node belongs a LinkedList.
//Test 11: Move a referenced node (fox) before the current node (dog):
//the quick brown jumps over the lazy old fox (dog)
//Test 12: Remove current node (dog) and attempt to indicate it:
//Node 'dog' is not in the list.
//Test 13: Add node removed in test 11 after a referenced node (brown):
//the quick brown (dog) jumps over the lazy old fox
//Test 14: Remove node that has the value 'old':
//the quick brown dog jumps over the lazy fox
//Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':
//the quick brown dog jumps over the lazy rhinoceros
//Test 16: Copy the list to an array:
//the
//quick
//brown
//dog
//jumps
//over
//the
//lazy
//rhinoceros
//Test 17: linked list Contains 'jumps'= True
//Test 18: Cleared linked list Contains 'jumps' = False
//
Imports System.Text
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create the link list.
Dim words() As String = {"the", "fox", _
"jumps", "over", "the", "dog"}
Dim sentence As New LinkedList(Of String)(words)
Console.WriteLine("sentence.Contains(""jumps"") = {0}", _
sentence.Contains("jumps"))
Display(sentence, "The linked list values:")
' Add the word 'today' to the beginning of the linked list.
sentence.AddFirst("today")
Display(sentence, "Test 1: Add 'today' to beginning of the list:")
' Move the first node to be the last node.
Dim mark1 As LinkedListNode(Of String) = sentence.First
sentence.RemoveFirst()
sentence.AddLast(mark1)
Display(sentence, "Test 2: Move first node to be last node:")
' Change the last node to 'yesterday'.
sentence.RemoveLast()
sentence.AddLast("yesterday")
Display(sentence, "Test 3: Change the last node to 'yesterday':")
' Move the last node to be the first node.
mark1 = sentence.Last
sentence.RemoveLast()
sentence.AddFirst(mark1)
Display(sentence, "Test 4: Move last node to be first node:")
' Indicate the last occurence of 'the'.
sentence.RemoveFirst()
Dim current As LinkedListNode(Of String) = sentence.FindLast("the")
IndicateNode(current, "Test 5: Indicate last occurence of 'the':")
' Add 'lazy' and 'old' after 'the' (the LinkedListNode named current).
sentence.AddAfter(current, "old")
sentence.AddAfter(current, "lazy")
IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':")
' Indicate 'fox' node.
current = sentence.Find("fox")
IndicateNode(current, "Test 7: Indicate the 'fox' node:")
' Add 'quick' and 'brown' before 'fox':
sentence.AddBefore(current, "quick")
sentence.AddBefore(current, "brown")
IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':")
' Keep a reference to the current node, 'fox',
' and to the previous node in the list. Indicate the 'dog' node.
mark1 = current
Dim mark2 As LinkedListNode(Of String) = current.Previous
current = sentence.Find("dog")
IndicateNode(current, "Test 9: Indicate the 'dog' node:")
' The AddBefore method throws an InvalidOperationException
' if you try to add a node that already belongs to a list.
Console.WriteLine("Test 10: Throw exception by adding node (fox) already in the list:")
Try
sentence.AddBefore(current, mark1)
Catch ex As InvalidOperationException
Console.WriteLine("Exception message: {0}", ex.Message)
End Try
Console.WriteLine()
' Remove the node referred to by mark1, and then add it
' before the node referred to by current.
' Indicate the node referred to by current.
sentence.Remove(mark1)
sentence.AddBefore(current, mark1)
IndicateNode(current, "Test 11: Move a referenced node (fox) before the current node (dog):")
' Remove the node referred to by current.
sentence.Remove(current)
IndicateNode(current, "Test 12: Remove current node (dog) and attempt to indicate it:")
' Add the node after the node referred to by mark2.
sentence.AddAfter(mark2, current)
IndicateNode(current, "Test 13: Add node removed in test 11 after a referenced node (brown):")
' The Remove method finds and removes the
' first node that that has the specified value.
sentence.Remove("old")
Display(sentence, "Test 14: Remove node that has the value 'old':")
' When the linked list is cast to ICollection(Of String),
' the Add method adds a node to the end of the list.
sentence.RemoveLast()
Dim icoll As ICollection(Of String) = sentence
icoll.Add("rhinoceros")
Display(sentence, "Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':")
Console.WriteLine("Test 16: Copy the list to an array:")
' Create an array with the same number of
' elements as the inked list.
Dim sArray() As String = New String((sentence.Count) - 1) {}
sentence.CopyTo(sArray, 0)
For Each s As String In sArray
Console.WriteLine(s)
Next
' Release all the nodes.
sentence.Clear()
Console.WriteLine()
Console.WriteLine("Test 17: Clear linked list. Contains 'jumps' = {0}", sentence.Contains("jumps"))
Console.ReadLine()
End Sub
Private Shared Sub Display(ByVal words As LinkedList(Of String), ByVal test As String)
Console.WriteLine(test)
For Each word As String In words
Console.Write((word + " "))
Next
Console.WriteLine()
Console.WriteLine()
End Sub
Private Shared Sub IndicateNode(ByVal node As LinkedListNode(Of String), ByVal test As String)
Console.WriteLine(test)
If IsNothing(node.List) Then
Console.WriteLine("Node '{0}' is not in the list." & vbLf, node.Value)
Return
End If
Dim result As StringBuilder = New StringBuilder(("(" _
+ (node.Value + ")")))
Dim nodeP As LinkedListNode(Of String) = node.Previous
While (Not (nodeP) Is Nothing)
result.Insert(0, (nodeP.Value + " "))
nodeP = nodeP.Previous
End While
node = node.Next
While (Not (node) Is Nothing)
result.Append((" " + node.Value))
node = node.Next
End While
Console.WriteLine(result)
Console.WriteLine()
End Sub
End Class
'This code example produces the following output:
'
'The linked list values:
'the fox jumps over the dog
'Test 1: Add 'today' to beginning of the list:
'today the fox jumps over the dog
'Test 2: Move first node to be last node:
'the fox jumps over the dog today
'Test 3: Change the last node to 'yesterday':
'the fox jumps over the dog yesterday
'Test 4: Move last node to be first node:
'yesterday the fox jumps over the dog
'Test 5: Indicate last occurence of 'the':
'the fox jumps over (the) dog
'Test 6: Add 'lazy' and 'old' after 'the':
'the fox jumps over (the) lazy old dog
'Test 7: Indicate the 'fox' node:
'the (fox) jumps over the lazy old dog
'Test 8: Add 'quick' and 'brown' before 'fox':
'the quick brown (fox) jumps over the lazy old dog
'Test 9: Indicate the 'dog' node:
'the quick brown fox jumps over the lazy old (dog)
'Test 10: Throw exception by adding node (fox) already in the list:
'Exception message: The LinkedList node belongs a LinkedList.
'Test 11: Move a referenced node (fox) before the current node (dog):
'the quick brown jumps over the lazy old fox (dog)
'Test 12: Remove current node (dog) and attempt to indicate it:
'Node 'dog' is not in the list.
'Test 13: Add node removed in test 11 after a referenced node (brown):
'the quick brown (dog) jumps over the lazy old fox
'Test 14: Remove node that has the value 'old':
'the quick brown dog jumps over the lazy fox
'Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':
'the quick brown dog jumps over the lazy rhinoceros
'Test 16: Copy the list to an array:
'the
'quick
'brown
'dog
'jumps
'over
'the
'lazy
'rhinoceros
'Test 17: Clear linked list. Contains 'jumps' = False
'
注解
LinkedList<T> 是常规用途链接列表。 它支持枚举器并实现 ICollection 接口,这与 .NET Framework 中的其他集合类一致。
LinkedList<T> 提供 LinkedListNode<T>类型的单独节点,因此插入和删除操作为 O(1) 操作。
可以删除节点,并在同一列表中或另一个列表中重新插入这些节点,这会导致堆上没有分配其他对象。 由于列表还维护内部计数,因此获取 Count 属性是 O(1) 操作。
LinkedList<T> 对象中的每个节点的类型为 LinkedListNode<T>。 由于 LinkedList<T> 是双重链接的,因此每个节点指向 Next 节点,然后向后指向 Previous 节点。
当同时创建节点及其值时,包含引用类型的列表的性能更好。
LinkedList<T> 接受 null
作为引用类型的有效 Value 属性,并允许重复值。
如果 LinkedList<T> 为空,则 First 和 Last 属性包含 null
。
LinkedList<T> 类不支持链接、拆分、周期或其他功能,这些功能可能会使列表处于不一致状态。 列表在单个线程上保持一致。 LinkedList<T> 支持的唯一多线程方案是多线程读取操作。
构造函数
LinkedList<T>() |
初始化 LinkedList<T> 类的新实例,该实例为空。 |
LinkedList<T>(IEnumerable<T>) |
初始化 LinkedList<T> 类的新实例,该实例包含从指定 IEnumerable 复制的元素,并且有足够的容量来容纳复制的元素数。 |
LinkedList<T>(SerializationInfo, StreamingContext) |
已过时.
初始化使用指定的 SerializationInfo 和 StreamingContext可序列化的 LinkedList<T> 类的新实例。 |
属性
Count |
获取实际包含在 LinkedList<T>中的节点数。 |
First |
获取 LinkedList<T>的第一个节点。 |
Last |
获取 LinkedList<T>的最后一个节点。 |
方法
显式接口实现
ICollection.CopyTo(Array, Int32) |
从特定 Array 索引开始,将 ICollection 的元素复制到 Array。 |
ICollection.IsSynchronized |
获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。 |
ICollection.SyncRoot |
获取可用于同步对 ICollection的访问的对象。 |
ICollection<T>.Add(T) |
在 ICollection<T>末尾添加项。 |
ICollection<T>.IsReadOnly |
获取一个值,该值指示 ICollection<T> 是否为只读。 |
IEnumerable.GetEnumerator() |
返回一个枚举器,该枚举器以集合的形式循环访问链接列表。 |
IEnumerable<T>.GetEnumerator() |
返回循环访问集合的枚举器。 |
扩展方法
适用于
线程安全性
此类型不是线程安全的。 如果需要多个线程访问 LinkedList<T>,则需要实现自己的同步机制。
只要集合未修改,LinkedList<T> 就可以同时支持多个读取器。 即便如此,通过集合进行枚举本质上不是线程安全的过程。 在枚举与写入访问竞争的极少数情况下,必须在整个枚举期间锁定集合。 若要允许多个线程访问集合进行读取和写入,必须实现自己的同步。