清除 C# 中的連結串列
使用 Clear() 方法清除 LinkedList。
我們首先來設定一個 LinkedList。
string [] employees = {"Patrick","Robert","John","Jacob", "Jamie"}; LinkedList<string> list = new LinkedList<string>(employees);
現在,讓我們清除 LinkedList。
list.Clear();
讓我們看看完整的程式碼。
示例
using System; using System.Collections.Generic; class Demo { static void Main() { string [] employees = {"Patrick","Robert","John","Jacob", "Jamie"}; LinkedList<string>list = new LinkedList<string>(employees); foreach (var emp in list) { Console.WriteLine(emp); } // clearing list list.Clear(); Console.WriteLine("LinkedList after removing the nodes (empty list)..."); foreach (var emp in list) { Console.WriteLine(emp); } } }
輸出
Patrick Robert John Jacob Jamie LinkedList after removing the nodes (empty list)...
廣告