刪除 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

更新於:2020 年 5 月 2 日

67 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始你的學習之旅
廣告
© . All rights reserved.