C#中的泛型链表的实现

来源:互联网 发布:update例子 mysql 编辑:程序博客网 时间:2024/05/30 04:29
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. namespace 泛型
  7. {
  8.     //简单链表的实现,单向(泛型)
  9.     class 泛型类
  10.     {
  11.         public static void Main()
  12.         {
  13.             
  14.             LinkedList<string> list = new LinkedList<string>();
  15.             list.AddLast("500");
  16.             list.AddLast("400");
  17.             list.AddLast("300");
  18.             list.AddLast("200");
  19.             //使用泛型版的IEnumerable时,foreach也是类型安全的,些时不可能写 string之外的类型
  20.             foreach (string i in list)
  21.             {
  22.                 Console.WriteLine(i);
  23.             }
  24.             Console.ReadLine();
  25.         }
  26.     }
  27.     ////泛型类的定义与一般类相似,只要使用泛型类型声明,之后,这个类型就可以做为一个字段成员,或者参数类型。
  28.     class LinkedListNode<T>
  29.     {
  30.         //项目值
  31.         private T value;
  32.         private LinkedListNode<T> next;
  33.         private LinkedListNode<T> prev;
  34.         public LinkedListNode(T value)
  35.         {
  36.             this.value = value;
  37.         }
  38.         public T Value
  39.         {
  40.             //只读属性
  41.             get
  42.             {
  43.                 return this.value;
  44.             }
  45.         }
  46.         //下一个
  47.         public LinkedListNode<T> Next
  48.         {
  49.             get { return next; }
  50.             set { next = value; }
  51.         }
  52.         //上一个
  53.         public LinkedListNode<T> Prev
  54.         {
  55.             get { return prev; }
  56.             set { prev = value; }
  57.         }
  58.     }
  59.     class LinkedList<T> : IEnumerable<T>
  60.     {
  61.         //第一个
  62.         private LinkedListNode<T> first;
  63.         internal LinkedListNode<T> First
  64.         {
  65.             get { return first; }
  66.             set { first = value; }
  67.         }
  68.         //最后一个
  69.         private LinkedListNode<T> last;
  70.         internal LinkedListNode<T> Last
  71.         {
  72.             get { return last; }
  73.             set { last = value; }
  74.         }
  75.         //从后面插入
  76.         public LinkedListNode<T> AddLast(T node)
  77.         {
  78.             LinkedListNode<T> newNode = new LinkedListNode<T>(node);
  79.             if (first == null)
  80.             {
  81.                 //Last的引用一直要更新
  82.                 first = newNode;
  83.                 last = first;
  84.             }
  85.             else
  86.             {
  87.                 //把当前最后一个节点的下一个节点的引用 指向新对象
  88.                 last.Next = newNode;
  89.                 //更新最后一个节点
  90.                 last = newNode;
  91.             }
  92.             return newNode;
  93.         }
  94.         public IEnumerator<T> GetEnumerator()
  95.         {
  96.             LinkedListNode<T> current = first;
  97.             while (current != null)
  98.             {
  99.                 yield return current.Value;
  100.                 //新引用指向下一个节点的地址
  101.                 current = current.Next;
  102.             }
  103.         }
  104.         IEnumerator IEnumerable.GetEnumerator()
  105.         {
  106.             return GetEnumerator();
  107.         }
  108.     }
  109. }