编程语言
首页 > 编程语言> > c#实现单链表的尾插法和头插法

c#实现单链表的尾插法和头插法

作者:互联网

数据结构很少有用c#实现的,为了方便c#的小伙伴们的学习,我打算把单链表中的尾插法和头插法用c#实现。

代码如下:

{
        static void Main(string[] args)
        {
            //WeiCha();
            TouCha();
        }

        static void WeiCha()
        {
            int d = int.Parse(Console.ReadLine().ToString());

            Node node = new Node();
            Node L = node;
            while (d!=-1)
            {
                Node s = new Node();
                s.val = d;
                node.next = s;
                node = s;
                d = int.Parse(Console.ReadLine().ToString());
            }
            L = L.next;
            while (L != null) { Console.WriteLine(L.val);L = L.next; }
            Console.ReadLine();

        }

        static void TouCha()
        {
            Node head = new Node();
            Node L = head;
            int d = int.Parse(Console.ReadLine().ToString());
            while (d!=-1)
            {
                Node s = new Node();
                s.val = d;
                s.next = head.next;
                head.next = s;
                d = int.Parse(Console.ReadLine().ToString());
            }
            L = L.next;
            while (L != null) { Console.WriteLine(L.val); L = L.next; }
            Console.ReadLine();

        }
    }

    class Node
    {
        public int val { set; get; }
        public Node next { set; get; }

    }

具体讲解我会在空闲时刻写出来

标签:Node,插法,单链,Console,val,c#,next,int,ReadLine
来源: https://www.cnblogs.com/qmz-blog/p/15902934.html