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
广告