设置一个LinkedList。
int [] num = {1, 2, 3, 4, 5};
LinkedList<int> list = new LinkedList<int>(num);现在,在usingAddLast()方法的末尾添加一个节点。
var newNode = list.AddLast(20);
要在上述添加的节点之后添加节点,请使用AddAfter()方法。
list.AddAfter(newNode, 30);
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
int [] num = {1, 2, 3, 4, 5};
LinkedList<int> list = new LinkedList<int>(num);
foreach (var n in list) {
Console.WriteLine(n);
}
//在最后添加一个节点
var newNode = list.AddLast(20);
//在上面添加的节点之后添加一个新节点
list.AddAfter(newNode, 30);
Console.WriteLine("LinkedList after adding new nodes...");
foreach (var n in list) {
Console.WriteLine(n);
}
}
}输出结果
1 2 3 4 5 LinkedList after adding new nodes... 1 2 3 4 5 20 30