C#中的LinkedList AddBefore方法
使用AddBefore()方法在C#中的给定节点之前添加一个节点。
我们的带有字符串节点的LinkedList。
string [] students = {"Henry","David","Tom"};
LinkedList<string> list = new LinkedList<string>(students);现在,让我们在最后添加节点。
//在最后添加一个节点
var newNode = list.AddLast("Brad");使用AddBefore()方法在上面添加的节点之前添加节点。
list.AddBefore(newNode, "Emma");
示例
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string [] students = {"Henry","David","Tom"};
LinkedList<string> list = new LinkedList<string>(students);
foreach (var stu in list) {
Console.WriteLine(stu);
}
//在最后添加一个节点
var newNode = list.AddLast("Brad");
//在上面添加的节点之前添加一个新节点
list.AddBefore(newNode, "Emma");
Console.WriteLine("LinkedList after adding new nodes...");
foreach (var stu in list) {
Console.WriteLine(stu);
}
}
}输出结果
Henry David Tom LinkedList after adding new nodes... Henry David Tom Emma Brad