Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 LinkedList 시작 부분에서 노드 제거

<시간/>

LinkedList의 시작 부분에서 노드를 제거하는 코드는 다음과 같습니다. -

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.RemoveFirst();
      Console.WriteLine("Count of nodes (UPDATED) = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED");
      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 (UPDATED) = 5
Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED Two
Three
Three
Three
Four

다른 예를 살펴보겠습니다 -

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.RemoveFirst();
      Console.WriteLine("Count of nodes (UPDATED) = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED");
      demoEnum = list.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
      list.RemoveFirst();
      Console.WriteLine("Count of nodes (UPDATED AGAIN) = " + list.Count);
      Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED AGAIN");
      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 (UPDATED) = 5
Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED
Two
Three
Three
Three
Four
Count of nodes (UPDATED AGAIN) = 4 Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED AGAIN
Three
Three
Three
Four