Để xóa một nút ở đầu LinkedList, hãy sử dụng phương thức RemoveFirst ().
string [] employees = {"Peter","Robert","John","Jacob"};
LinkedList<string> list = new LinkedList<string>(employees); Bây giờ, để loại bỏ phần tử đầu tiên, hãy sử dụng phương thức RemoveFirst ().
list.RemoveFirst();
Hãy để chúng tôi xem ví dụ đầy đủ.
Ví dụ
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string [] employees = {"Peter","Robert","John","Jacob"};
LinkedList<string> list = new LinkedList<string>(employees);
foreach (var emp in list) {
Console.WriteLine(emp);
}
// removing first node
list.RemoveFirst();
Console.WriteLine("LinkedList after removing first node...");
foreach (var emp in list) {
Console.WriteLine(emp);
}
}
} Đầu ra
Peter Robert John Jacob LinkedList after removing first node... Robert John Jacob