要在LinkedList中找到包含指定值的第一个节点,代码如下-
using System; using System.Collections.Generic; public class Demo { public static void Main(){ LinkedList<string> list = new LinkedList<string>(); list.AddLast("John"); list.AddLast("Tim"); list.AddLast("Kevin"); list.AddLast("Jacob"); list.AddLast("Emma"); list.AddLast("Ryan"); list.AddLast("Brad"); list.AddLast("Carl"); Console.WriteLine("LinkedList elements..."); foreach(string str in list){ Console.WriteLine(str); } LinkedListNode<string> val = list.Find("Jacob"); Console.WriteLine("Specified value = "+val.Value); } }
输出结果
这将产生以下输出-
LinkedList elements... John Tim Kevin Jacob Emma Ryan Brad Carl Specified value = Jacob
现在让我们来看另一个示例-
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); Console.WriteLine("LinkedList elements..."); foreach(int i in list){ Console.WriteLine(i); } LinkedListNode<int> val = list.Find(300); Console.WriteLine("Specified value = "+val.Value); } }
输出结果
这将产生以下输出-
LinkedList elements... 100 200 300 400 500 Specified value = 300