要在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); } }
输出结果
这将产生以下输出-
LinkedList elements... 100 200 300 400 500 300 500 Specified value = 300
让我们看另一个例子-
using System; using System.Collections.Generic; public class Demo { public static void Main() { LinkedList<String> list = new LinkedList<String>(); list.AddLast("One"); list.AddLast("Two"); list.AddLast("Three"); list.AddLast("Four"); list.AddLast("Five"); Console.WriteLine("Elements in LinkedList..."); foreach (string res in list) { Console.WriteLine(res); } LinkedListNode<string> val = list.FindLast("Five"); Console.WriteLine("Specified value = "+val.Value); } }
输出结果
这将产生以下输出-
Elements in LinkedList... One Two Three Four Five Specified value = Five