ขั้นแรก ตั้งค่า LinkedList ด้วยโหนด
string [] students = {"Tim","Jack","Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students);
หากต้องการเพิ่มโหนดที่ตำแหน่งแรก ให้ใช้เมธอด AddFirst()
list.AddFirst("Amit");
ตัวอย่าง
using System; using System.Collections.Generic; class Demo { static void Main() { string [] students = {"Tim","Jack","Henry","David","Tom"}; LinkedList<string> list = new LinkedList<string>(students); foreach (var stu in list) { Console.WriteLine(stu); } // adding a node Console.WriteLine("LinkedList after adding a node at the first position..."); list.AddFirst("Amit"); foreach (var stu in list) { Console.WriteLine(stu); } } }
ผลลัพธ์
Tim Jack Henry David Tom LinkedList after adding a node at the first position... Amit Tim Jack Henry David Tom