Đầu tiên, tạo một danh sách liên kết mới -
LinkedList<string> myList = new LinkedList<string>();
Bây giờ, hãy thêm một số phần tử vào danh sách được liên kết -
// 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"); Bây giờ chúng ta hãy tìm một nút và thêm một nút mới sau đó -
LinkedListNode<string> node = myList.Find("R");
myList.AddAfter(node, "ADDED"); Ví dụ
Bạn có thể thử chạy đoạn mã sau để tìm một nút trong danh sách được liên kết.
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);
}
}
} Đầu ra
P Q R ADDED S T U