Đặt bộ sưu tập danh sách liên kết -
var list = new LinkedList<string>();
Bây giờ, hãy thêm các phần tử -
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Four"); Bây giờ, hãy để chúng tôi thêm các phần tử mới trong LinkedList đã được tạo -
LinkedListNode<String> node = list.Find("Four");
list.AddBefore(node, "Three");
list.AddAfter(node, "Five"); Bây giờ chúng ta hãy xem cách đi qua các nút trong Danh sách được liên kết đơn lẻ -
Ví dụ
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
var list = new LinkedList < string > ();
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Four");
Console.WriteLine("Travering...");
foreach(var res in list) {
Console.WriteLine(res);
}
LinkedListNode < String > node = list.Find("Four");
list.AddBefore(node, "Three");
list.AddAfter(node, "Five");
Console.WriteLine("Travering after adding new elements...");
foreach(var res in list) {
Console.WriteLine(res);
}
}
}