Đặt một danh sách liên kết.
int [] num = {1, 2, 3, 4, 5};
LinkedList<int> list = new LinkedList<int>(num); Bây giờ, hãy thêm một nút vào cuối bằng phương thức AddLast ().
var newNode = list.AddLast(20);
Để thêm một nút sau nút đã thêm ở trên, hãy sử dụng phương thức AddAfter ().
list.AddAfter(newNode, 30);
Ví dụ
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
int [] num = {1, 2, 3, 4, 5};
LinkedList<int> list = new LinkedList<int>(num);
foreach (var n in list) {
Console.WriteLine(n);
}
// adding a node at the end
var newNode = list.AddLast(20);
// adding a new node after the node added above
list.AddAfter(newNode, 30);
Console.WriteLine("LinkedList after adding new nodes...");
foreach (var n in list) {
Console.WriteLine(n);
}
}
} Đầu ra
1 2 3 4 5 LinkedList after adding new nodes... 1 2 3 4 5 20 30