C#学习之泛型

  1 //主函数//主函数里面调用的类都在后面有具体描述
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace GeneralType
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             LinkedListNode<int> sub = new LinkedListNode<int>(200);
 15             LinkedListNode<int> sub1 = new LinkedListNode<int>(300);
 16             LinkedListNode<int> sub2 = new LinkedListNode<int>(400);
 17             sub.next = sub1;
 18             sub.prev = null;
 19             sub1.prev = sub;
 20             sub1.next = sub2;
 21             sub2.prev = sub1;
 22             sub2.next = null;
 23             LinkedListNode<int> point;
 24             point = sub2;
 25             while(point!=null)
 26             {
 27               string text=  point.Value.ToString();
 28                 Console.WriteLine(text);
 29                 point = point.prev;
 30             }
 31             Console.ReadLine();
 32             //上面是创建节点类的实例,下面是创建了一个链表的实例
 33             var link = new LinkList<int>();
 34             link.Addlast(100);
 35             link.Addlast(200);
 36             link.Addlast(300);
 37             link.Addlast(400);
 38             foreach(int i in link)
 39             {
 40                 Console.WriteLine(i);
 41             }
 42             Console.ReadLine();
 43             return ;
 44         }
 45 
 46     }
 47 }
 48 //以下是建立了两个类,分别在不同的文件中//LinkedListNode<T>
 49 using System;
 50 using System.Collections.Generic;
 51 using System.Linq;
 52 using System.Text;
 53 using System.Threading.Tasks;
 54 
 55 namespace GeneralType
 56 {
 57     public class LinkedListNode<T>
 58     {
 59         //创建节点类,对象可以使全部类型
 60         //本类具有值Value 的属性
 61         //值需要在初始化的时候加入,不能再类外部通过其他方式对
 62         //本类一个对象的值赋值
 63         //本类包含两个引用属性,一个是指向本节点在链表中前面的节点,一个是指向后面的节点
 64         //可以在本类中使用
 65         //Internal修饰符是指在本程序集内可以赋值
 66         public LinkedListNode(T value)
 67         {
 68             this.Value=value;
 69         }
 70         public T Value
 71         {
 72             private set;
 73             get;
 74         }
 75         public LinkedListNode<T> prev
 76         {
 77             get;
 78             internal set;
 79         }
 80         public LinkedListNode<T> next
 81         {
 82             get;
 83             internal set;
 84         }
 85     }
 86 }
 87 //LinkList<T>
 88 using System;
 89 using System.Collections.Generic;
 90 using System.Linq;
 91 using System.Text;
 92 using System.Threading.Tasks;
 93 using System.Collections;
 94 
 95 namespace GeneralType
 96 {
 97    public class LinkList<T>:IEnumerable<T>
 98     {
 99         //首先先为本类创建两个属性,First and Last
100         public LinkedListNode<T> First { get; private set; }
101         public LinkedListNode<T> Last { get; private set; }
102        //下面的函数式在向建立的链表对象中添加数据
103        //同时返回这个数据所建立的新的节点对象
104         public LinkedListNode<T> Addlast(T one)
105         {
106             var newnode = new LinkedListNode<T>(one);
107             if(First==null)
108             {
109                 First = newnode;
110                 Last = newnode;
111                 First.next = Last;
112                 Last.prev = First;
113             }
114             else
115             {
116                 Last.next = newnode;
117                 newnode.prev = Last;
118                 Last = newnode;
119             }
120             return newnode;
121         }
122        //通过实现GetEnumerable()方法可以使用foreach 遍历链表
123        //yield return和yield break 可以多次出现,但是不能用return语句
124        public IEnumerator<T> GetEnumerator()
125         {
126             LinkedListNode<T> current = this.First;
127            while(current!=null)
128            {
129                yield return current.Value;
130                //Console.WriteLine(current.ToString());
131                current = current.next;
132            }
133         }
134        IEnumerator IEnumerable.GetEnumerator()
135         {
136        return GetEnumerator();
137         }
138     }
139 }

 

posted @ 2014-09-20 20:55  SYTM  阅读(336)  评论(0编辑  收藏  举报