首先,设置带有节点的LinkedList。
string [] students = {"Tim","Jack","Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students);
要在第一个位置添加节点,请使用AddFirst()
方法。
list.AddFirst("Amit");
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添加节点 at the first position..."); list.AddFirst("Amit"); foreach (var stu in list) { Console.WriteLine(stu); } } }
输出结果
Tim Jack Henry David Tom LinkedList after添加节点 at the first position... Amit Tim Jack Henry David Tom