AddBefore() 메서드를 사용하여 C#에서 주어진 노드 앞에 노드를 추가합니다.
문자열 노드가 있는 LinkedList.
string [] students = {"Henry","David","Tom"};
LinkedList<string> list = new LinkedList<string>(students); 이제 마지막에 노드를 추가해 보겠습니다.
// adding a node at the end
var newNode = list.AddLast("Brad"); 위에서 추가한 노드 앞에 AddBefore() 메서드를 사용하여 노드를 추가합니다.
list.AddBefore(newNode, "Emma");
예시
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string [] students = {"Henry","David","Tom"};
LinkedList<string> list = new LinkedList<string>(students);
foreach (var stu in list) {
Console.WriteLine(stu);
}
// adding a node at the end
var newNode = list.AddLast("Brad");
// adding a new node before the node added above
list.AddBefore(newNode, "Emma");
Console.WriteLine("LinkedList after adding new nodes...");
foreach (var stu in list) {
Console.WriteLine(stu);
}
}
} 출력
Henry David Tom LinkedList after adding new nodes... Henry David Tom Emma Brad