C#程序在链表中给定节点之后添加节点
设置一个LinkedList并添加元素。
string [] students = {"Beth","Jennifer","Amy","Vera"};
LinkedList<string> list = new LinkedList<string>(students);首先,在末尾添加一个新节点。
var newNode = list.AddLast("Emma");现在,使用该AddAfter()方法在给定节点之后添加一个节点。
list.AddAfter(newNode, "Matt");
以下是完整的代码。
示例
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string [] students = {"Beth","Jennifer","Amy","Vera"};
LinkedList<string> list = new LinkedList<string>(students);
foreach (var stu in list) {
Console.WriteLine(stu);
}
//在最后添加一个节点
var newNode = list.AddLast("Emma");
//在上面添加的节点之后添加一个新节点
list.AddAfter(newNode, "Matt");
Console.WriteLine("LinkedList after adding new nodes...");
foreach (var stu in list) {
Console.WriteLine(stu);
}
}
}输出结果
Beth Jennifer Amy Vera LinkedList after adding new nodes... Beth Jennifer Amy Vera Emma Matt
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短