C#程序删除链接列表末尾的节点
以下是我们的LinkedList。
string [] employees = {"Patrick","Robert","John","Jacob", "Jamie"}; LinkedList<string> list = new LinkedList<string>(employees);
现在,假设您需要删除最后一个节点,即“Jamie”。为此,请使用RemoveLast()
方法。
list.RemoveLast();
示例
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); } //删除最后一个节点 list.RemoveLast(); Console.WriteLine("LinkedList after删除最后一个节点..."); foreach (var emp in list) { Console.WriteLine(emp); } } }
输出结果
Patrick Robert John Jacob Jamie LinkedList after删除最后一个节点... Patrick Robert John Jacob