Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C# LinkedList에서 특정 값의 첫 번째 항목만 제거하는 방법

C#에서 LinkedList<T> 컬렉션에 동일한 값이 여러 개 포함되어 있을 때, Remove() 메서드를 호출하면 해당 값이 처음으로 나타나는 노드 단 하나만 제거됩니다. 나머지 중복 노드는 그대로 유지됩니다. 이 메서드는 노드 제거에 성공하면 true, 목록에서 값을 찾지 못하면 false를 반환하며, 값을 찾기 위해 처음부터 순회하므로 시간 복잡도는 O(n)입니다.

예제 1

아래 예제에서는 문자열 "A"가 4개 포함된 LinkedList를 만든 뒤, Remove("A")를 한 번 호출하여 맨 앞의 "A" 노드만 제거하는 과정을 보여줍니다.

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      LinkedList<string> list = new LinkedList<string>();
      list.AddLast("A");
      list.AddLast("B");
      list.AddLast("C");
      list.AddLast("A");
      list.AddLast("E");
      list.AddLast("F");
      list.AddLast("A");
      list.AddLast("H");
      list.AddLast("A");
      list.AddLast("j");

      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");

      LinkedList<string>.Enumerator demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }

      list.Remove("A");

      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");

      demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

Count of nodes = 10
Elements in LinkedList... (Enumerator iterating through LinkedList)
A
B
C
A
E
F
A
H
A
j
Count of nodes = 9
Elements in LinkedList... (Enumerator iterating through LinkedList)
B
C
A
E
F
A
H
A
j

출력을 보면 노드 수가 10개에서 9개로 줄었고, 맨 앞에 있던 "A" 하나만 사라진 것을 확인할 수 있습니다. 뒤쪽에 남아 있던 세 개의 "A"는 그대로 유지됩니다.

예제 2

이번에는 동일한 값 "Three"가 3번 반복되는 경우를 살펴보겠습니다.

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      LinkedList<string> list = new LinkedList<string>();
      list.AddLast("One");
      list.AddLast("Two");
      list.AddLast("Three");
      list.AddLast("Three");
      list.AddLast("Three");
      list.AddLast("Four");

      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");

      LinkedList<string>.Enumerator demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }

      list.Remove("Three");

      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");

      demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

Count of nodes = 6
Elements in LinkedList... (Enumerator iterating through LinkedList)
One
Two
Three
Three
Three
Four
Count of nodes = 5
Elements in LinkedList... (Enumerator iterating through LinkedList)
One
Two
Three
Three
Four

정리

LinkedList<T>.Remove(T value) 메서드의 주요 특징은 다음과 같습니다.

  • 값이 처음 발견되는 노드 하나만 제거하고, 나머지 중복 노드는 그대로 유지합니다.
  • 제거에 성공하면 true, 값이 존재하지 않으면 false를 반환합니다.
  • 값을 찾기 위해 목록을 처음부터 순회하므로 시간 복잡도는 O(n)입니다.
  • 모든 중복 항목을 제거하려면 반환값이 true인 동안 Remove()를 반복 호출해야 합니다.