在C#中LinkedList的末尾添加新节点或值
要在LinkedList的末尾添加新节点或值,代码如下-
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<String> list = new LinkedList<String>();
list.AddLast("A");
list.AddLast("B");
list.AddLast("C");
list.AddLast("D");
list.AddLast("E");
list.AddLast("F");
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("Elements in LinkedList...");
foreach (string res in list) {
Console.WriteLine(res);
}
list.AddLast("G");
list.AddLast("H");
list.AddLast("I");
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("Elements in LinkedList...");
foreach (string res in list) {
Console.WriteLine(res);
}
}
}输出结果
这将产生以下输出-
Count of nodes = 6 Elements in LinkedList... A B C D E F Count of nodes = 9 Elements in LinkedList... A B C D E F G H I
示例
让我们看另一个例子-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<int> list = new LinkedList<int>();
list.AddLast(100);
list.AddLast(200);
list.AddLast(300);
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("Elements in LinkedList...");
foreach (int res in list) {
Console.WriteLine(res);
}
list.AddLast(400);
list.AddLast(500);
list.AddLast(600);
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("Elements in LinkedList...");
foreach (int res in list) {
Console.WriteLine(res);
}
}
}输出结果
这将产生以下输出-
Count of nodes = 3 Elements in LinkedList... 100 200 300 Count of nodes = 6 Elements in LinkedList... 100 200 300 400 500 600
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西