从 C# 中的 LinkedList 中删除指定节点?
要从 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
广告