国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

c# 自定義泛型鏈表類的詳解

2020-01-24 03:15:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
(1)自定義泛型鏈表類。
復(fù)制代碼 代碼如下:

public class GenericList<T>
    {
        private class Node
        {
            //當(dāng)前節(jié)點(diǎn)值
            private T data;
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
            //節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
            private Node next;
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
            //節(jié)點(diǎn)的上一個(gè)節(jié)點(diǎn)
            private Node last;
            public Node Last
            {
                get { return last; }
                set { last = value; }
            }
            public Node(T t)
            {
                data = t;
                next = null;
            }
        }
        private Node firstNode;
        private Node lastNode;
        public void AddNode(T t)
        {
            Node node = new Node(t);
            node.Last = lastNode;
            if (lastNode != null)
                lastNode.Next = node;
            lastNode = node;
            if (firstNode == null)
            {
                firstNode = node;
            }
        }
        //要在自定義泛型集合上迭代
        //必須實(shí)現(xiàn)該接口
        public IEnumerator<T> GetEnumerator()
        {
            Node current = firstNode;
            while (current != null)
            {
                //yield return表達(dá)式以枚舉對(duì)象返回
                yield return current.Data;
                current = current.Next;
            }
        }
    }

 (2)自定義泛型鏈表類調(diào)用。
復(fù)制代碼 代碼如下:

class GenericListTestTwo
    {
        static void Main()
        {
            // 類型參數(shù)為int
            GenericList<int> list = new GenericList<int>();
            for (int a = 0; a < 5; a++)
            {
                list.AddNode(a);
            }
            foreach (int i in list)
            {
                System.Console.WriteLine(i);
            }
            //類型參數(shù)為string
            GenericList<string> strList = new GenericList<string>();
            strList.AddNode("First Node");
            strList.AddNode("Second Node");
            foreach(string s in strList)
            {
                System.Console.WriteLine(s);
            }
            Console.Read();
        }
    }

輸出如下:

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黑龙江省| 上蔡县| 图木舒克市| 永新县| 木兰县| 镇雄县| 黄梅县| 奎屯市| 淮北市| 阜新市| 汝南县| 吴江市| 河池市| 中超| 武城县| 云林县| 来宾市| 西贡区| 桃园市| 定结县| 南漳县| 盐边县| 大港区| 阳曲县| 黔西| 安阳县| 左云县| 左权县| 盘锦市| 靖州| 永寿县| 延庆县| 龙游县| 云安县| 八宿县| 济阳县| 安西县| 柳河县| 恩施市| 海口市| 宣武区|