
首先,创建一个新的链表 -
LinkedList<string> myList = new LinkedList<string>();
现在在链接列表中添加一些元素 -
// Add 6 elements in the linked list
myList.AddLast("P");
myList.AddLast("Q");
myList.AddLast("R");
myList.AddLast("S");
myList.AddLast("T");
myList.AddLast("U");现在让我们找到一个节点并在其后添加一个新节点 -
LinkedListNode<string> node = myList.Find("R");
myList.AddAfter(node, "ADDED");示例
您可以尝试运行以下代码来查找链表中的节点。
现场演示
using System;
using System.Collections.Generic;
class Program {
static void Main() {
LinkedList<string> myList = new LinkedList<string>();
// Add 6 elements in the linked list
myList.AddLast("P");
myList.AddLast("Q");
myList.AddLast("R");
myList.AddLast("S");
myList.AddLast("T");
myList.AddLast("U");
LinkedListNode<string> node = myList.Find("R");
myList.AddAfter(node, "ADDED");
foreach (var i in myList) {
Console.WriteLine(i);
}
}
}
输出
P Q R ADDED S T U
