먼저 노드가 있는 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