C# 程式:移除連結串列中開頭的節點
要移除連結串列開頭的節點,請使用 RemoveFirst() 方法。
string [] employees = {"Peter","Robert","John","Jacob"}; LinkedList<string> list = new LinkedList<string>(employees);
現在,要移除第一個元素,請使用 RemoveFirst() 方法。
list.RemoveFirst();
我們來看一個完整的例子。
示例
using System; using System.Collections.Generic; class Demo { static void Main() { string [] employees = {"Peter","Robert","John","Jacob"}; LinkedList<string> list = new LinkedList<string>(employees); foreach (var emp in list) { Console.WriteLine(emp); } // removing first node list.RemoveFirst(); Console.WriteLine("LinkedList after removing first node..."); foreach (var emp in list) { Console.WriteLine(emp); } } }
輸出
Peter Robert John Jacob LinkedList after removing first node... Robert John Jacob
廣告