ขั้นแรก สร้างรายการเชื่อมโยงใหม่ -
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