要从LinkedList中删除指定的节点,代码如下-
using System; using System.Collections.Generic; public class Demo { public static void Main() { LinkedList<int> list = new LinkedList<int>(); list.AddLast(100); list.AddLast(200); list.AddLast(300); list.AddLast(400); list.AddLast(500); list.AddLast(300); list.AddLast(500); Console.WriteLine("LinkedList elements..."); foreach(int i in list) { Console.WriteLine(i); } LinkedListNode<int> val = list.FindLast(300); Console.WriteLine("Specified value = "+val.Value); list.Remove(500); Console.WriteLine("LinkedList elements...UPDATED"); foreach(int i in list) { Console.WriteLine(i); } } }
输出结果
这将产生以下输出-
LinkedList elements... 100 200 300 400 500 300 500 Specified value = 300 LinkedList elements...UPDATED 100 200 300 400 300 500
让我们看另一个例子-
using System; using System.Collections.Generic; public class Demo { public static void Main() { LinkedList<string> list = new LinkedList<string>(); list.AddLast("Mark"); list.AddLast("David"); list.AddLast("Harry"); list.AddLast("John"); list.AddLast("Kevin"); string[] strArr = new string[5]; list.CopyTo(strArr, 0); Console.WriteLine("LinkedList elements...after copying to array"); foreach(string str in strArr) { Console.WriteLine(str); } list.Remove("Harry"); Console.WriteLine("LinkedList elements...UPDATED"); foreach(string str in list) { Console.WriteLine(str); } } }
输出结果
这将产生以下输出-
LinkedList elements...after copying to array Mark David Harry John Kevin LinkedList elements...UPDATED Mark David John Kevin