自定义表链 SnakList

 

 

using System;
using System.Collections;

namespace Galaxy
{
    class Program
    {
        //https://blog.csdn.net/qq_40223639/article/details/105129935
        static void Main(string[] args)
        {
            SnakList<string> san = new();
               
        string xiaoming = "小米";
            string xiaoming1 = "1小米";
            san.SnakListAdd("dfdfdfsf1");
            san.SnakListAdd("dfdfdfsf2");
            san.SnakListAdd("dfdfdfsf3");
            san.SnakListAdd(xiaoming);
            san.SnakListAdd(xiaoming1);
            Console.WriteLine(san.Length);
            foreach (var sa in san)
            {

                Console.WriteLine(sa);


            }
        }

    }
    public class SnakList<T> : IEnumerable
    {

        private Node SnakHead = null;//表链头
        private int length = 0;//计数器
        private Node snakTail = null;
        private Node current =null;// 游标
        public int Length { get => length; set => length = value; }

        public void SnakListAdd(T Tn)
        {
           
            Node snakTail = new(Tn);

            Node A = new Node ();

            if ( length==0)
            {

                SnakHead = snakTail;
               
               
               
            }
            else
            {

                A = SnakHead;
                while (A.Next != null)
                {
                    A = A.Next;
                }
                A.Next = snakTail;


            }

            this.Length += 1;

        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            current = SnakHead;
            while (current is not null)
            {
                yield return current;

                current = current.Next;
            }

        }
    
      

       public T this[T sd]
        {
            get
            {

                Node se = new(sd);


                current = SnakHead;
                while (current is not null)
                {
                    if (current.Date.Equals(se.Date)) return current.Date;

                    current = current.Next;
                }
                return default;

            }
        }

        //嵌套类不能申明为public类型, 使嵌套类型在外部可见违反了代码质量规则 CA1034
        //嵌套类不需要用 泛型,因为外类的泛型参数在子类可以使用
        class Node
        {
            private T date;
            private Node next;
            public Node()
            {
            }

            public Node(T node)
            {


                this.date = node;
                Next = null;


            }

            public T Date { get => date; set => date = value; }
            public Node Next { get => next; set => next = value; }
            public override string ToString()
            {
                return date.ToString();


            }
        }

    }



}

 

上一篇:光纤交换机产品功能介绍


下一篇:存储技术: DAS NAS SAN