c# 自定义泛型链表类的详解

时间:2022-07-27 23:58:46 

(1)自定义泛型链表类。


public class GenericList<T>
    {
        private class Node
        {
            //当前节点值
            private T data;
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
            //节点的下一个节点
            private Node next;
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
            //节点的上一个节点
            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;
            }
        }
        //要在自定义泛型集合上迭代
        //必须实现该接口
        public IEnumerator<T> GetEnumerator()
        {
            Node current = firstNode;
            while (current != null)
            {
                //yield return表达式以枚举对象返回
                yield return current.Data;
                current = current.Next;
            }
        }
    }


 (2)自定义泛型链表类调用。


class GenericListTestTwo
    {
        static void Main()
        {
            // 类型参数为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);
            }
            //类型参数为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();
        }
    }


输出如下:

c# 自定义泛型链表类的详解

标签:c#,自定义泛型,链表类
0
投稿

猜你喜欢

  • c# 类成员初始化顺序的特殊情况

    2021-07-04 18:04:45
  • Java如何通过枚举实现有限状态机

    2021-08-05 04:21:05
  • c#扩展datatable转json示例

    2022-08-19 10:44:55
  • java操作excel表格详解

    2021-08-20 14:35:46
  • springboot整合spring-retry的实现示例

    2022-01-13 01:25:53
  • C#自定义类型强制转换实例分析

    2022-01-14 08:20:06
  • MPAndroidChart 自定义图表绘制使用实例

    2023-08-08 13:58:41
  • JAVA求两直线交点和三角形内外心的方法

    2023-07-30 02:46:35
  • 详解AOP与Filter拦截请求打印日志实用例子

    2021-09-26 22:03:10
  • Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)

    2023-08-06 01:01:56
  • android实现滚动文本效果

    2022-02-02 16:49:36
  • Java 全面系统介绍反射的运用

    2021-12-18 22:51:30
  • android的got表HOOK实现代码

    2023-11-18 17:04:51
  • C#中的委托和事件

    2023-11-27 12:33:34
  • java 8如何自定义收集器(collector)详解

    2022-02-12 07:22:17
  • SparkSQL开窗函数分析使用示例

    2022-04-16 02:26:32
  • SpringBoot上传图片到指定位置并返回URL的实现

    2023-08-13 14:02:52
  • Android实现短信验证功能的代码

    2022-08-28 19:54:15
  • 谈谈Java中自定义注解及使用场景

    2022-08-28 04:45:39
  • 解决@Cacheable在同一个类中方法调用不起作用的问题

    2022-02-19 01:46:54
  • asp之家 软件编程 m.aspxhome.com