LinkedList包含C#中的方法
这是我们的LinkedList。
int [] num = {1, 3, 7, 15};
LinkedList<int> list = new LinkedList<int>(num);若要检查列表是否包含元素,请使用Contains()方法。以下示例检查列表中的节点3。
list.Contains(3)
上面,由于找到了元素,因此返回True,如下所示-
示例
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
int [] num = {1, 3, 7, 15};
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);
}
Console.WriteLine("Is number 3 (node) in the list?: "+list.Contains(3));
}
}输出结果
1 3 7 15 LinkedList after adding new nodes... 1 3 7 15 20 30 Is number 3 (node) in the list?: True