设置一个链接列表集合-
var list = new LinkedList<string>();
现在,添加元素-
list.AddLast("One"); list.AddLast("Two"); list.AddLast("Four");
现在,让我们在已经创建的LinkedList中添加新元素-
LinkedListNode<String> node = list.Find("Four"); list.AddBefore(node, "Three"); list.AddAfter(node, "Five");
现在让我们看看如何遍历单链列表中的节点-
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); } } }