在 C# 中移除連結串列中的所有節點


要從 LinkedList 中移除所有節點,程式碼如下 -

示例

 即時演示

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int [] num = {10, 20, 30, 40, 50};
      LinkedList<int> list = new LinkedList<int>(num);
      Console.WriteLine("LinkedList nodes...");
      foreach (var n in list) {
         Console.WriteLine(n);
      }
      list.Clear();
      Console.WriteLine("LinkedList is empty now!");
      foreach (var n in list) {
         Console.WriteLine(n);
      }
   }
}

輸出

這將產生以下輸出 -

LinkedList nodes...
10
20
30
40
50
LinkedList is empty now!

示例

我們來看另一個示例 -

 即時演示

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("D");
      list.AddLast("E");
      list.AddLast("F");
      list.AddLast("G");
      list.AddLast("H");
      list.AddLast("I");
      list.AddLast("J");
      Console.WriteLine("Count of nodes = " + list.Count);
      list.Clear();
      Console.WriteLine("Count of nodes (updated) = " + list.Count);
   }
}

輸出

這將產生以下輸出 -

Count of nodes = 10
Count of nodes (updated) = 0

更新日期: 2019-12-04

148 次瀏覽

啟動你的 職業

完成課程即可獲得認證

開始
廣告