이 항목은 아직 평가되지 않았습니다.- 이 항목 평가

LinkedList 제네릭 클래스

참고: 이 클래스는 .NET Framework 버전 2.0에서 새로 추가되었습니다.

이중으로 링크된 목록을 나타냅니다.

네임스페이스: System.Collections.Generic
어셈블리: System(system.dll)

[SerializableAttribute] 
[ComVisibleAttribute(false)] 
public class LinkedList<T> : ICollection<T>, IEnumerable<T>, 
	ICollection, IEnumerable, ISerializable, IDeserializationCallback
J#에서는 제네릭 형식 및 메서드를 사용할 수 있지만 새로 선언할 수는 없습니다.
JScript에서는 제네릭 형식 및 메서드를 지원하지 않습니다.

Type 매개 변수

T

링크된 목록의 요소 형식을 지정합니다.

LinkedList은 일반 용도의 링크된 목록입니다. 이 목록은 .NET Framework의 다른 컬렉션 클래스와 마찬가지로 열거자를 지원하고 ICollection 인터페이스를 구현합니다.

LinkedList은 별도의 LinkedListNode 형식 노드가 있는 실제 링크된 목록이므로 삽입 및 제거는 O(1) 연산입니다. 참조 형식이 포함된 목록은 노드와 해당 값을 동시에 만들 때 보다 향상된 성능을 제공합니다. 내부 노드가 노출되므로 동일한 목록이나 다른 목록에서 GC 힙에 대한 할당이 없는 O(1) 연산으로 노드를 제거하고 다시 삽입할 수 있습니다. 이 목록은 또한 Count 속성 가져오기가 O(1) 연산이 되도록 내부 개수를 유지합니다. 단일 스레드에서 목록이 사용되는 경우에도 개수는 일관된 상태로 유지됩니다.

LinkedList 클래스는 목록을 일관되지 않은 상태로 만들 수 있는 연결, 분할, 주기, 목록 또는 기타 기능을 지원하지 않습니다. 설계에서는 목록의 무결성 유지를 우선으로 하므로 단일 스레드의 작업에서는 목록이 일관된 상태로 유지됩니다. LinkedList에서 지원하는 다중 스레드 시나리오는 다중 스레드 읽기 작업뿐입니다. 다른 다중 스레드 시나리오에서는 사용자 고유 동기화를 제공해야 합니다.

Note참고

특정 상황에서 보다 향상된 성능을 제공하고 추가 기능을 안전하게 프로그래밍할 수 있는 링크된 목록은 언제든지 만들 수 있습니다.

LinkedList의 각 노드는 LinkedListNode 형식입니다. LinkedList은 이중으로 링크되므로 각 노드는 앞쪽으로 Next 노드와 링크되고 뒤쪽으로 Previous 노드와 링크됩니다. 노드에는 목록에 대한 링크가 있으므로 실제로 다른 목록에 있는 노드를 제거하려고 하는 것과 같이 목록에서 잘못된 작업을 수행해도 일관된 상태가 유지됩니다.

LinkedList은 Null 참조(Visual Basic의 경우 Nothing)을 참조 형식에 대해 유효한 Value로 받아들이며 중복 값을 허용합니다.

LinkedList이 비어 있으면 FirstLast 속성에는 Null 참조(Visual Basic의 경우 Nothing)이 포함됩니다.

다음 코드 예제에서는 LinkedList 클래스의 여러 기능을 보여 줍니다. 이 예제에서는 문자열 배열을 만든 다음 이 문자열 배열을 LinkedList(제네릭 IEnumerable) 생성자에 전달하여 문자열의 LinkedList을 만들고 채웁니다.

이 코드 예제에서는 결과로 생성된 링크된 목록을 LinkedList 클래스의 속성 및 메서드를 사용하여 조작하고 각 작업의 결과를 표시합니다.

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

System.Object
  System.Collections.Generic.LinkedList

이 형식의 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 모든 인스턴스 멤버는 스레드로부터 안전하지 않을 수 있습니다.

LinkedList는 컬렉션이 수정되지 않으면 여러 개의 reader를 동시에 지원할 수 있습니다. 그렇다 하더라도 컬렉션을 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 드물지만 열거가 쓰기 권한과 경쟁하는 경우 전체 열거 중에 컬렉션을 잠가야 합니다. 여러 스레드에서 컬렉션에 액세스하여 읽고 쓸 수 있도록 허용하려면 사용자 지정 동기화를 구현해야 합니다.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

.NET Framework

2.0에서 지원

.NET Compact Framework

2.0에서 지원
이 정보가 도움이 되었습니까?
(1500자 남음)
커뮤니티 콘텐츠 추가