============================================================非泛型
創(chuàng)新互聯(lián)專注于靈山企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城建設(shè)。靈山網(wǎng)站建設(shè)公司,為靈山等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站建設(shè),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
-------------------------LinkedListNode.cs類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class LinkedListNode { //上一個對象 //下一個對象 //值 public LinkedListNode Prev { get; set; } public LinkedListNode Next { get; set; } public object Value { get; private set; } public LinkedListNode(object o) { this.Value = o; } } }
-------------------------LinkedList.cs類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { public class LinkedList:IEnumerable { //頭部 //尾部 public LinkedListNode First { get; set; } public LinkedListNode Last { get; set; } public LinkedListNode AddValue(object o) { LinkedListNode lln = new LinkedListNode(o); if (First == null) { First = lln; Last = lln; } else { LinkedListNode buffer = Last;//緩存上一個對象 Last.Next = lln;//設(shè)置上一個對象的Next(指定上一個對象的下一個對象是lln) Last = lln;//設(shè)置尾部對象為lln Last.Prev = buffer;//設(shè)置lln的上一個對象 } return lln; } public IEnumerator GetEnumerator() { LinkedListNode lln = First; while (lln != null) { yield return lln.Value; lln = lln.Next; } } } }
-------------------------主方法
static void Main(string[] args) { LinkedList ll = new LinkedList(); ll.AddValue("asdas"); ll.AddValue("sad"); ll.AddValue("asdsdaas"); ll.AddValue("asddsadaasdasdasas"); foreach (object o in ll) { Console.WriteLine(o); Console.ReadKey(); } }
缺點:不斷的裝箱拆箱,數(shù)據(jù)類型不安全,代碼性能差
============================================================泛型
-------------------------LinkListNode.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class LinkListNode{ //上一對象 //下一對象 //值 public LinkListNode Prev { get; set; } public LinkListNode Next { get; set; } public T Value { get; private set; } public LinkListNode(T value) { this.Value = value; } } }
-------------------------LinkList.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { public class LinkList:IEnumerable { //頭部 //尾部 public LinkListNode First { get; set; } public LinkListNode Last { get; set; } public LinkListNode AddList(T value) { LinkListNode lln = new LinkListNode (value); if (First == null) { First = lln; Last = lln; } else { LinkListNode buffer = Last; Last.Next = lln; Last = lln; Last.Prev = buffer; } return lln; } public IEnumerator GetEnumerator() { LinkListNode lln = First; while (lln != null) { yield return lln.Value; lln = lln.Next; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }
-------------------------主方法
static void Main(string[] args) { var i =new LinkList(); i.AddList(1); i.AddList(2); foreach (int l in i) { Console.WriteLine(l); Console.ReadKey(); } }