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) |
已淘汰.
初始化 LinkedList<T> 類別的新實例,這個實例可使用指定的 SerializationInfo 和 StreamingContext串行化。 |
屬性
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> 就可以同時支援多個讀取器。 即便如此,透過集合列舉本質上不是安全線程的程式。 在列舉與寫入存取權競爭的罕見情況下,整個列舉期間必須鎖定集合。 若要允許多個線程存取集合以進行讀取和寫入,您必須實作自己的同步處理。