เพื่อให้ได้ตัวแจงนับที่วนซ้ำผ่าน LinkedList รหัสจะเป็นดังนี้ -
ตัวอย่าง
using System; using System.Collections.Generic; public class Demo { public static void Main(){ LinkedList<string> list = new LinkedList<string>(); list.AddLast("A"); list.AddLast("B"); list.AddLast("C"); list.AddLast("D"); list.AddLast("E"); list.AddLast("F"); list.AddLast("G"); list.AddLast("H"); list.AddLast("I"); list.AddLast("J"); Console.WriteLine("Count of nodes = " + list.Count); Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)"); LinkedList<string>.Enumerator demoEnum = list.GetEnumerator(); while (demoEnum.MoveNext()) { string res = demoEnum.Current; Console.WriteLine(res); } } }
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Count of nodes = 10 Elements in LinkedList... (Enumerator iterating through LinkedList) A B C D E F G H I J
ตัวอย่าง
เรามาดูตัวอย่างกัน −
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args){ LinkedList<string> list1 = new LinkedList<string>(); list1.AddLast("One"); list1.AddLast("Two"); list1.AddLast("Three"); list1.AddLast("Four"); list1.AddLast("Five"); Console.WriteLine("Elements in LinkedList1..."); foreach (string res in list1){ Console.WriteLine(res); } LinkedList<string> list2 = new LinkedList<string>(); list2.AddLast("India"); list2.AddLast("US"); list2.AddLast("UK"); list2.AddLast("Canada"); list2.AddLast("Poland"); list2.AddLast("Netherlands"); Console.WriteLine("Elements in LinkedList2... (Enumerator iterating through LinkedList2)"); LinkedList<string>.Enumerator demoEnum = list2.GetEnumerator(); while (demoEnum.MoveNext()) { string res = demoEnum.Current; Console.WriteLine(res); } LinkedList<string> list3 = new LinkedList<string>(); list3 = list2; Console.WriteLine("Is LinkedList3 equal to LinkedList2? = "+list3.Equals(list2)); } }
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Elements in LinkedList1... One Two Three Four Five Elements in LinkedList2... (Enumerator iterating through LinkedList2) India US UK Canada Poland Netherlands Is LinkedList3 equal to LinkedList2? = True