设置带有节点的LinkedList。
string [] students = {"Tim","Jack","Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students);
现在,使用该AddLast()
方法在最后一个位置添加节点。
list.AddLast("Kevin");
这是带有更新的LinkedList的完整代码。
using System; using System.Collections.Generic; class Demo { static void Main() { string [] students = {"Tim","Jack","Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students); foreach (var stu in list) { Console.WriteLine(stu); } //在最后添加一个节点 Console.WriteLine("LinkedList after adding a node at the last position..."); list.AddLast("Kevin"); foreach (var stu in list) { Console.WriteLine(stu); } } }
输出结果
Tim Jack Henry David Tom LinkedList after adding a node at the last position... Tim Jack Henry David Tom Kevin