.NET自從2.0版本開始就支持泛型。
- 非泛型鏈表
閑話休提,馬上來看下非泛型的簡化鏈表類,它可以包含任意類型的對象。
LinkedListNode.cs中:
在鏈表中,一個元素引用另一個元素,所以必須創建一個類,將其封裝在鏈表中,並引用下一個對象。
1 public class LinkedListNode 2 { 3 public LinkedListNode(object value) 4 { 5 this.Value = value; 6 } 7 8 public object Value { get; private set; } 9 10 public LinkedListNode Next { get; internal set; } 11 public LinkedListNode Prev { get; internal set; } 12 }
LinkedListNode.cs中:
LinkedList類包含LinkedListNode類型的First,與Last屬性,它們分別標志了鏈表的頭尾。通過實現GetEnumerator()方法,可以用foreach語句遍歷鏈表。
1 public class LinkedList : IEnumerable 2 { 3 public LinkedListNode First { get; private set; } 4 public LinkedListNode Last { get; private set; } 5 6 public LinkedListNode AddLast(object node) 7 { 8 var newNode = new LinkedListNode(node); 9 if (First == null) 10 { 11 First = newNode; 12 Last = First; 13 } 14 else 15 { 16 Last.Next = newNode; 17 Last = newNode; 18 } 19 return newNode; 20 } 21 22 public IEnumerator GetEnumerator() 23 { 24 LinkedListNode current = First; 25 while (current != null) 26 { 27 yield return current.Value; 28 current = current.Next; 29 } 30 }
Program.cs中:
1 var list1 = new LinkedList(); 2 list1.AddLast(2); 3 list1.AddLast(4); 4 list1.AddLast("6"); 5 6 foreach (int i in list1) 7 { 8 Console.WriteLine(i); 9 }
此時,會出現一個運行異常,因為把鏈表中第3個元素轉換成整形時會出現異常。
- 泛型鏈表
為了避免這種情況,下面創建泛型的鏈表。
LinkedListNode.cs中:
LinkedListNode類用一個泛型類型T聲明。屬性Value的類型是The,而不是object.
public class LinkedListNode<T> { public LinkedListNode(T value) { this.Value = value; } public T Value { get; private set; } public LinkedListNode<T> Next { get; internal set; } public LinkedListNode<T> Prev { get; internal set; } }
LinkedList.cs中:
也把LinkedList定義成泛型類。
1 public class LinkedList<T> : IEnumerable<T> 2 { 3 public LinkedListNode<T> First { get; private set; } 4 public LinkedListNode<T> Last { get; private set; } 5 6 public LinkedListNode<T> AddLast(T node) 7 { 8 var newNode = new LinkedListNode<T>(node); 9 if (First == null) 10 { 11 First = newNode; 12 Last = First; 13 } 14 else 15 { 16 Last.Next = newNode; 17 Last = newNode; 18 } 19 return newNode; 20 } 21 22 public IEnumerator<T> GetEnumerator() 23 { 24 LinkedListNode<T> current = First; 25 26 while (current != null) 27 { 28 yield return current.Value; 29 current = current.Next; 30 } 31 } 32 33 IEnumerator IEnumerable.GetEnumerator() 34 { 35 return GetEnumerator(); 36 } 37 }
Program.cs中:
1 class Program 2 { 3 static void Main() 4 { 5 var list2 = new LinkedList<int>(); 6 list2.AddLast(1); 7 list2.AddLast(3); 8 list2.AddLast(5); 9 10 foreach (int i in list2) 11 { 12 Console.WriteLine(i); 13 } 14 15 var list3 = new LinkedList<string>(); 16 list3.AddLast("2"); 17 list3.AddLast("four"); 18 list3.AddLast(null); 19 20 foreach (string s in list3) 21 { 22 Console.WriteLine(s); 23 } 24 Console.Read(); 25 26 }
現在foreach是類型安全的了。