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) |
從特定的 ICollection 索引開始,將 Array 的項目複製到 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>可以同時支援多個讀取器,只要集合未修改即可。 即使如此,透過集合列舉本質上不是安全線程的程式。 在列舉與寫入存取競爭的罕見情況下,必須在整個列舉期間鎖定集合。 若要讓多重執行緒能夠存取集合以便進行讀取和寫入,您必須實作自己的同步處理。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應