C#中的泛型鏈表的實現

來源:互聯網
上載者:User
  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. }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.