• [原创]c#,单链表,数据结构 Virus


     //链表结点
        class SingleLink
        {
            private int idata;
            public int Data
            {
                get
                { return idata; }
                set
                { idata = value; }
            }
            public SingleLink next;

            public SingleLink()
            {
                idata = -1;
                next = null;
            }

            public SingleLink(int data)
            {
                idata = data;
                next = null;
            }
        }

        //链表
        class SingleLinkList
        {
            private SingleLink head;
            private SingleLink rear;
            private SingleLinkList[] my=new SingleLinkList[100];
            public SingleLinkList()
            {
                head = new SingleLink();
                rear = new SingleLink();
            }

            public SingleLinkList this [int index]
            {
                get
                {
                    return my[index];
                }
            }

            public bool IsEmpty()
            {
                return (head.next==null);
            }

            public  SingleLink FindSingleLink(int search)
            {
                SingleLink p = new SingleLink();
                p = head;
                if (p.next == null)
                {
                    Console.WriteLine("the link is empty.");
                    return null;
                }
                while (p.next != null)
                {
                    if (p.Data == search)
                    {
                        return p;
                    }
                    p=p.next;
                }
                Console.WriteLine("the link have not the "+search+".");
                return null;
            }

            public bool Insert(int data)
            {
                if (head.next == null)
                {
                    return InsertAfterHead(data);
                }
                SingleLink newlink = new SingleLink(data);
                newlink.Data = data;
                rear.next = newlink;
                rear = newlink;
                return true;
            }

            public bool InsertAfter(int search, int data)
            {
                if (head.next == null)
                {
                    Console.WriteLine("this link is empty.");
                    return false;
                    //return InsertAfterHead(data);
                }
                SingleLink x =FindSingleLink(search);

                if (x == null)
                {
                    Console.WriteLine("this link have not the " + search+".");
                    return false;
                }
               
                SingleLink newlink = new SingleLink(data);
                newlink.Data = data;
                newlink.next = x.next;
                x.next = newlink;
                Console.WriteLine("insert is successful.");
                return true;
            }

            private bool InsertAfterHead(int data)
            {
                Console.WriteLine("this is the first element.");
                SingleLink newlink = new SingleLink(data);
                newlink.Data = data;
                head.next = newlink;
                rear = newlink;
               
                Console.WriteLine("the first element insert successful!");
                return true;
            }

            public void Display()
            {
                SingleLink p=new SingleLink();
                p=head;
                if (p.next == null)
                {
                    Console.WriteLine("this link is empty.");
                    return;
                }
                p = head.next;
                do
                {
                    Console.WriteLine(p.Data.ToString());
                    p = p.next;
                } while (p != null);
              
            }

        }

    class Program

        {
            static void Main(string[] args)
            {
                SingleLinkList list = new SingleLinkList();
                Console.WriteLine(list.IsEmpty());
                list.InsertAfter(4, 4);
                list.Display();
                list.Insert(4);
                list.Display();
                list.Insert(7);
                list.Insert(3);
                list.Insert(9);
                list.InsertAfter(5, 2);
                list.InsertAfter(7, 8);
                list.Display();

                Console.ReadLine();
            }

         }

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    javaEE项目部署方式
    Docker安装mysql5.6
    自定义镜像mycentos
    DockerFile体系结构(保留字指令)
    Cognition math based on Factor Space (2016.05)
    MATLAB画ROC曲线,及计算AUC值
    MATLAB时间序列预测Prediction of time series with NAR neural network
    因素空间发展评述
    ps 证件照(1,2寸)
    kali linux wmtools安装
  • 原文地址:https://www.cnblogs.com/virusswb/p/853700.html
Copyright © 2020-2023  润新知