C# 中的 LinkedList Clear() 方法

csharpprogrammingserver side programming更新于 2025/8/11 6:07:17

使用 Clear() 方法清除 LinkedList。这将删除 LinkedList 中的所有节点。

假设以下是我们的 LinkedList −

int [] num = {30, 65, 80, 95, 110, 135};
LinkedList<int> list = new LinkedList<int>(num);

清除 LinkedList。

list.Clear();

示例

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      int [] num = {30, 65, 80, 95, 110, 135};
      LinkedList<int> list = new LinkedList<int>(num);
      foreach (var n in list) {
         Console.WriteLine(n);
      }
      // clear
      list.Clear();
      Console.WriteLine("No node in the LinkedList now...");
      foreach (var n in list) {
         Console.WriteLine(n);
      }
   }
}

输出

30
65
80
95
110
135
No node in the LinkedList now...

相关文章