LinkedList ジェネリック クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > LinkedList ジェネリック クラスの意味・解説 

LinkedList ジェネリック クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

二重リンクされリスト表します

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(False)> _
Public Class LinkedList(Of
 T)
    Implements ICollection(Of T), IEnumerable(Of
 T), _
    ICollection, IEnumerable, ISerializable, IDeserializationCallback
[SerializableAttribute] 
[ComVisibleAttribute(false)] 
public class LinkedList<T> : ICollection<T>,
 IEnumerable<T>, 
    ICollection, IEnumerable, ISerializable, IDeserializationCallback
[SerializableAttribute] 
[ComVisibleAttribute(false)] 
generic<typename T>
public ref class LinkedList : ICollection<T>,
 IEnumerable<T>, 
    ICollection, IEnumerable, ISerializable, IDeserializationCallback
J# では、ジェネリックな型およびメソッド使用できますが、新規に宣言することはできません。
JScript では、ジェネリックな型およびメソッド使用できません。

型パラメータ

T

リンク リスト要素の型を示します

解説解説

LinkedList は、汎用のリンク リストです。これは、.NET Framework の他のコレクション クラス同様に列挙子をサポートし、ICollection インターフェイス実装しています。

LinkedList は、LinkedListNode 型の個別ノードを持つ真のリンク リストであるため、挿入および削除は O(1) 操作なります参照型格納しているリストは、ノードとその値が同時に作成される場合、より効率的に実行されます。基になるノード公開されているため、GC ヒープ上の割り当て行わない O(1) 操作としてノード削除し、それらを同じリストまたは別のリストに再挿入できますリストには内部カウント維持されるため、Count プロパティ取得は O(1) 操作です。リストシングル スレッドから使用される場合は、このカウント一貫性保たれます。

LinkedList クラスでは、リスト一貫性のない状態にするチェーン分割循環参照などの機能サポートしていません。この設計により、リスト整合性維持でき、シングル スレッドでの操作では、リスト一貫性保たれます。LinkedListマルチスレッドサポートされているのは、マルチスレッド読み取り操作場合だけです。それ以外マルチスレッド操作場合は、独自の同期処理を提供する必要があります

メモメモ

常に、特定の状況でより効率的に実行されるリンク リストや、追加機能のためにプログラミング安全性犠牲にするリンク リスト作成される可能性あります

LinkedList の各ノードは、LinkedListNode 型です。LinkedList二重リンクされているため、各ノードは、前方Next ノード後方Previous ノード指してます。ノードにはリストへのリンクがあるため、そのリストに対して無効な操作 (実際に別のリスト存在するノード削除しようとするなど) を行っても、リスト矛盾した状態にはなりません。

LinkedList は、null 参照 (Visual Basic では Nothing) を参照型に対して有効な Value として受け取り、値の重複許可します

LinkedList が空の場合First プロパティLast プロパティには null 参照 (Visual Basic では Nothing) が格納されます。

使用例使用例

LinkedList クラス備え多数機能次の例で示します。このコード例では、文字列配列作成し、その文字列配列LinkedList(ジェネリック IEnumerable) コンストラクタに渡すことによって、文字列LinkedList作成しデータ読み込みます。

また、LinkedList クラスプロパティおよびメソッド使用して結果リンク リスト操作し操作結果表示します

Imports System
Imports System.Text
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim words() As String
 = _
            { "the", "fox",
 "jumped", "over",
 "the", "dog" }
        Dim sentence As New
 LinkedList(Of String)(words)
        Display(sentence)

        Console.WriteLine("sentence.Contains(""jumped"")
 = {0}", _
            sentence.Contains("jumped"))

        ' Add the word "today" to the beginning of the linked
 list.
        ' Remove the new node, and add it to the end of the list.
        sentence.AddFirst("today")
        Display(sentence)

        Dim mark1 As LinkedListNode(Of
 String) = sentence.First
        sentence.RemoveFirst()
        sentence.AddLast(mark1)
        Display(sentence)

        sentence.RemoveLast()
        sentence.AddLast("yesterday")
        Display(sentence)

        mark1 = sentence.Last
        sentence.RemoveLast()
        sentence.AddFirst(mark1)
        Display(sentence)

        sentence.RemoveFirst()

        Dim current As LinkedListNode(Of
 String) = _
            sentence.FindLast("the")
        DisplayNode(current)

        sentence.AddAfter(current, "old")
        sentence.AddAfter(current, "lazy")
        DisplayNode(current)

        current = sentence.Find("fox")
        DisplayNode(current)

        sentence.AddBefore(current, "quick")
        sentence.AddBefore(current, "brown")
        DisplayNode(current)

        ' Keep a reference to the current node, "fox", and
 to the
        ' previous node in the list. Use the Find method to locate
        ' the node containing the value "dog". Show the position.
        mark1 = current
        Dim mark2 As LinkedListNode(Of
 String) = current.Previous
        current = sentence.Find("dog")
        DisplayNode(current)

        ' The AddBefore method throws an InvalidOperationException
        ' if you try to add a node that already belongs to a list.
        Try
            sentence.AddBefore(current, mark1)
        Catch ex As InvalidOperationException
            Console.WriteLine("Exception message: {0}",
 ex.Message)
        End Try

        ' Remove the node referred to by mark1, and add it before 
        ' the node referred to by current. Show the sentence, 
        ' highlighting the position of the node referred to by
        ' current.
        sentence.Remove(mark1)
        sentence.AddBefore(current, mark1)
        DisplayNode(current)

        ' Remove the node referred to by current. If you try to show
        ' its position now, the DisplayNode method prints a message.
        ' Add the node after the node referred to by mark2, and 
        ' display the sentence, highlighting current.
        sentence.Remove(current)
        DisplayNode(current)
        sentence.AddAfter(mark2, current)
        DisplayNode(current)

        ' The Remove method finds and removes the first node that 
        ' that has the specified value.
        sentence.Remove("old")
        Display(sentence)

        ' 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)

        ' Create an array, specifying a size one less than the size
        ' of the linked list, because Visual Basic allocates one 
        ' more element than you specify.
        Console.WriteLine(vbLf & "Copy the list to an array.")
        Dim sArray(sentence.Count - 1) As String
 
        sentence.CopyTo(sArray, 0)

        For Each s As String
 In sArray
            Console.WriteLine(s)
        Next

        ' Release all the nodes.
        sentence.Clear()

    End Sub

    Private Shared Sub Display(ByVal
 words As LinkedList(Of String))
        For Each word As
 String In words
            Console.Write(word & " ")
        Next
        Console.WriteLine()
    End Sub
    
    Private Shared Sub DisplayNode(ByVal
 node As LinkedListNode(Of String))
        If node.List Is Nothing
 Then
            Console.WriteLine("Node ""{0}""
 is not in a list.", node.Value)
            Return
        End If

        Dim result As New
 StringBuilder ("(" & node.Value & ")")
        Dim nodeP As LinkedListNode(Of
 String) = node.Previous

        While nodeP IsNot Nothing
            result.Insert(0, nodeP.Value & " ")
            nodeP = nodeP.Previous
        End While

        node = node.Next
        While node IsNot Nothing
            result.Append(" " & node.Value)
            node = node.Next
        End While

        Console.WriteLine(result)
    End Sub

End Class

'This code example produces the following output:
'
'the fox jumped over the dog
'sentence.Contains("jumped") = True
'today the fox jumped over the dog
'the fox jumped over the dog today
'the fox jumped over the dog yesterday
'yesterday the fox jumped over the dog
'the fox jumped over (the) dog
'the fox jumped over (the) lazy old dog
'the (fox) jumped over the lazy old dog
'the quick brown (fox) jumped over the lazy old dog
'the quick brown fox jumped over the lazy old (dog)
'Exception message: The LinkedList node belongs a LinkedList.
'the quick brown jumped over the lazy old fox (dog)
'Node "dog" is not in a list.
'the quick brown (dog) jumped over the lazy old fox
'the quick brown dog jumped over the lazy fox
'the quick brown dog jumped over the lazy rhinoceros
'
'Copy the list to an array.
'the
'quick
'brown
'dog
'jumped
'over
'the
'lazy
'rhinoceros
using System;
using System.Text;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        string[] words = 
            {"the", "fox", "jumped", "over",
 "the", "dog"};
        LinkedList<string> sentence = new
 LinkedList<string>(words);
        Display(sentence);

        Console.WriteLine("sentence.Contains(\"jumped\") = {0}",
 
            sentence.Contains("jumped"));

        // Add the word "today" to the beginning of the linked
 list.
        // Remove the new node, and add it to the end of the list.
        sentence.AddFirst("today");
        Display(sentence);

        LinkedListNode<string> mark1 = sentence.First;
        sentence.RemoveFirst();
        sentence.AddLast(mark1);
        Display(sentence);

        sentence.RemoveLast();
        sentence.AddLast("yesterday");
        Display(sentence);

        mark1 = sentence.Last;
        sentence.RemoveLast();
        sentence.AddFirst(mark1);
        Display(sentence);

        sentence.RemoveFirst();

        LinkedListNode<string> current = sentence.FindLast("the");
        DisplayNode(current);

        sentence.AddAfter(current, "old");
        sentence.AddAfter(current, "lazy");
        DisplayNode(current);

        current = sentence.Find("fox");
        DisplayNode(current);

        sentence.AddBefore(current, "quick");
        sentence.AddBefore(current, "brown");
        DisplayNode(current);

        // Keep a reference to the current node, "fox", and
 to the
        // previous node in the list. Use the Find method to locate
        // the node containing the value "dog". Show the position.
        mark1 = current;
        LinkedListNode<string> mark2 = current.Previous;
        current = sentence.Find("dog");
        DisplayNode(current);

        // The AddBefore method throws an InvalidOperationException
        // if you try to add a node that already belongs to a list.
        try
        {
            sentence.AddBefore(current, mark1);
        }
        catch(InvalidOperationException ex)
        {
            Console.WriteLine("Exception message: {0}", ex.Message);
        }

        // Remove the node referred to by mark1, and add it before 
        // the node referred to by current. Show the sentence, 
        // highlighting the position of the node referred to by
        // current.
        sentence.Remove(mark1);
        sentence.AddBefore(current, mark1);
        DisplayNode(current);

        // Remove the node referred to by current. If you try to show
        // its position now, the DisplayNode method prints a message.
        // Add the node after the node referred to by mark2, and 
        // display the sentence, highlighting current.
        sentence.Remove(current);
        DisplayNode(current);
        sentence.AddAfter(mark2, current);
        DisplayNode(current);

        // The Remove method finds and removes the first node that 
        // that has the specified value.
        sentence.Remove("old");
        Display(sentence);

        // 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);

        // Create an array with the same number of elements as the
        // linked list.
        Console.WriteLine("\nCopy the list to an array.");
        string[] sArray = new string[sentence.Count];
        sentence.CopyTo(sArray, 0);

        foreach( string s in
 sArray )
        {
            Console.WriteLine(s);
        }

        // Release all the nodes.
        sentence.Clear();

    }

    private static void
 Display(LinkedList<string> words)
    {
        foreach( string word in
 words )
        {
            Console.Write(word + " ");
        }
        Console.WriteLine();
    }
    
    private static void
 DisplayNode(LinkedListNode<string> node)
    {
        if (node.List==null)
        {
            Console.WriteLine("Node \"{0}\" is not in
 a list.", 
                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);
    }
}

//This code example produces the following output:
//
//the fox jumped over the dog
//sentence.Contains("jumped") = True
//today the fox jumped over the dog
//the fox jumped over the dog today
//the fox jumped over the dog yesterday
//yesterday the fox jumped over the dog
//the fox jumped over (the) dog
//the fox jumped over (the) lazy old dog
//the (fox) jumped over the lazy old dog
//the quick brown (fox) jumped over the lazy old dog
//the quick brown fox jumped over the lazy old (dog)
//Exception message: The LinkedList node belongs a LinkedList.
//the quick brown jumped over the lazy old fox (dog)
//Node "dog" is not in a list.
//the quick brown (dog) jumped over the lazy old fox
//the quick brown dog jumped over the lazy fox
//the quick brown dog jumped over the lazy rhinoceros
//
//Copy the list to an array.
//the
//quick
//brown
//dog
//jumped
//over
//the
//lazy
//rhinoceros
#using <System.dll>

using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;

void Display(LinkedList<String^>^ words)
{
    for each( String^ word in words )
    {
        Console::Write(word + " ");
    }
    Console::WriteLine();
};
    
void DisplayNode(LinkedListNode<String^>^ node)
{
    if (node->List == nullptr)
    {
        Console::WriteLine("Node \"{0}\" is not in
 a list.", 
            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);
};

void main()
{
    array<String^>^ words = 
        {"the", "fox", "jumped", "over",
 "the", "dog"};
    LinkedList<String^>^ sentence = 
        gcnew LinkedList<String^>((IEnumerable<String^>^) words);
    Display(sentence);

    Console::WriteLine("sentence->Contains(\"jumped\") = {0}",
 
        sentence->Contains("jumped"));

    // Add the word "today" to the beginning of the linked
 list.
    // Remove the new node, and add it to the end of the list.
    sentence->AddFirst("today");
    Display(sentence);

    LinkedListNode<String^>^ mark1 = sentence->First;
    sentence->RemoveFirst();
    sentence->AddLast(mark1);
    Display(sentence);

    sentence->RemoveLast();
    sentence->AddLast("yesterday");
    Display(sentence);

    mark1 = sentence->Last;
    sentence->RemoveLast();
    sentence->AddFirst(mark1);
    Display(sentence);

    sentence->RemoveFirst();

    LinkedListNode<String^>^ current = sentence->FindLast("the");
    DisplayNode(current);

    sentence->AddAfter(current, "old");
    sentence->AddAfter(current, "lazy");
    DisplayNode(current);

    current = sentence->Find("fox");
    DisplayNode(current);

    sentence->AddBefore(current, "quick");
    sentence->AddBefore(current, "brown");
    DisplayNode(current);

    // Keep a reference to the current node, "fox", and to
 the
    // previous node in the list. Use the Find method to locate
    // the node containing the value "dog". Show the position.
    mark1 = current;
    LinkedListNode<String^>^ mark2 = current->Previous;
    current = sentence->Find("dog");
    DisplayNode(current);

    // The AddBefore method throws an InvalidOperationException
    // if you try to add a node that already belongs to a list.
    try
    {
        sentence->AddBefore(current, mark1);
    }
    catch(InvalidOperationException^ ex)
    {
        Console::WriteLine("Exception message: {0}", ex->Message);
    }

    // Remove the node referred to by mark1, and add it before 
    // the node referred to by current. Show the sentence, 
    // highlighting the position of the node referred to by
    // current.
    sentence->Remove(mark1);
    sentence->AddBefore(current, mark1);
    DisplayNode(current);

    // Remove the node referred to by current. If you try to show
    // its position now, the DisplayNode method prints a message.
    // Add the node after the node referred to by mark2, and 
    // display the sentence, highlighting current.
    sentence->Remove(current);
    DisplayNode(current);
    sentence->AddAfter(mark2, current);
    DisplayNode(current);

    // The Remove method finds and removes the first node that 
    // that has the specified value.
    sentence->Remove("old");
    Display(sentence);

    // 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);

    // Create an array with the same number of elements as the
    // linked list.
    Console::WriteLine("\nCopy the list to an array.");
    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();
}

//This code example produces the following output:
//
//the fox jumped over the dog
//sentence->Contains("jumped") = True
//today the fox jumped over the dog
//the fox jumped over the dog today
//the fox jumped over the dog yesterday
//yesterday the fox jumped over the dog
//the fox jumped over (the) dog
//the fox jumped over (the) lazy old dog
//the (fox) jumped over the lazy old dog
//the quick brown (fox) jumped over the lazy old dog
//the quick brown fox jumped over the lazy old (dog)
//Exception message: The LinkedList node belongs a LinkedList.
//the quick brown jumped over the lazy old fox (dog)
//Node "dog" is not in a list.
//the quick brown (dog) jumped over the lazy old fox
//the quick brown dog jumped over the lazy fox
//the quick brown dog jumped over the lazy rhinoceros
//
//Copy the list to an array.
//the
//quick
//brown
//dog
//jumped
//over
//the
//lazy
//rhinoceros
継承階層継承階層
System.Object
  System.Collections.Generic.LinkedList
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
LinkedList メンバ
System.Collections.Generic 名前空間
LinkedListNode


このページでは「.NET Framework クラス ライブラリ リファレンス」からLinkedList ジェネリック クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からLinkedList ジェネリック クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からLinkedList ジェネリック クラス を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「LinkedList ジェネリック クラス」の関連用語

LinkedList ジェネリック クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



LinkedList ジェネリック クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS