• C#泛型链表Demo


        /// <summary>
        /// 节点
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class LinkedListNode<T>
        {
            public LinkedListNode(T value)
            {
                this.Value = value;
            }
            public T Value { get; private set; }//
            /// <summary>
            /// 后一个节点
            /// </summary>
            public LinkedListNode<T> Next { get; internal set; }
            /// <summary>
            /// 前一个节点
            /// </summary>
            public LinkedListNode<T> Prev { get; internal set; }
        }
     public class LinkedList<T> : IEnumerable<T>
        {
            public LinkedListNode<T> First { get; private set; }
            public LinkedListNode<T> Last { get; private set; }
            
            public LinkedListNode<T> AddLast(T node)
            {
                var newNode = new LinkedListNode<T>(node);
                if(First == null)
                {
                    First = newNode;
                    Last = First;
                }
                else
                {
                    LinkedListNode<T> previous = Last;
                    Last.Next = newNode;
                    Last = newNode;
                    Last.Prev = previous;
                }
                return newNode;
            }
            public IEnumerator<T> GetEnumerator()
            {
                LinkedListNode<T> current = First;
                while (current!=null)
                {
                    yield return current.Value;
                    current = current.Next;
                }
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }
     static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("**********************LinkedList<int>*********************");
                    var list1 = new LinkedList<int>();
                    list1.AddLast(1);
                    list1.AddLast(3);
                    list1.AddLast(5);
                    list1.AddLast(7);
                    foreach (var item in list1)
                    {
                        Console.WriteLine(item);
                    }
                    Console.WriteLine("*********************LinkedList<string>**********************");
                    var list2 = new LinkedList<string>();
                    list2.AddLast("2");
                    list2.AddLast("aaaa");
                    list2.AddLast("bbbbb");
                    list2.AddLast("cccc");
                    foreach (var item in list2)
                    {
                        Console.WriteLine(item);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("程序出现错误:" + ex.Message);
                }
    
                Console.ReadKey();
            }
  • 相关阅读:
    关于global和$GLOBALS[]的一些实践
    java环境配置的新手教程
    echart图表 resize()方法使用
    使用git上传下载项目
    windows 系统新建 vue 项目的坑
    Java版求1000以内的完全数
    Java版经典兔子繁殖迭代问题——斐波那契(Fibonacci)数列
    Java版冒泡排序和选择排序
    AngularJS 动画总结
    Mac下sublime text 的“package control”安装
  • 原文地址:https://www.cnblogs.com/marshhu/p/6658760.html
Copyright © 2020-2023  润新知