1.3.23

上次更新:2019-04-17
发现了题解错误/代码缺陷/排版问题?请点这里:如何:提交反馈

解答

由于先后问题,y 在第一句代码执行完毕之后无法访问,t 的 next 会指向自己。

代码

using System;
using Generics;

namespace _1._3._23
{
    /*
     * 1.3.23
     * 
     * 为什么下面这段代码和上一题中的代码效果不同?
     * 
     */
    class Program
    {
        //x.next = t        x 的下一个是 t
        //t.next = x.next   t 的下一个和 x 的下一个相同(也就是 t)
        //于是 t.next = t, 遍历会出现死循环。
        static void Main(string[] args)
        {
            Node<string> first = new Node<string>();
            Node<string> second = new Node<string>();
            Node<string> third = new Node<string>();
            Node<string> fourth = new Node<string>();

            first.item = "first";
            second.item = "second";
            third.item = "third";
            fourth.item = "fourth";

            first.next = second;
            second.next = third;
            third.next = fourth;
            fourth.next = null;

            Node<string> current = first;
            while (current != null)
            {
                Console.Write(current.item + " ");
                current = current.next;
            }

            Node<string> t = new Node<string>();
            t.item = "t";

            second.next = t;
            t.next = second.next;

            Console.WriteLine();

            current = first;
            while (current != null)
            {
                Console.Write(current.item + " ");
                current = current.next;
            }
        }
    }
}

另请参阅

Generics 库

上一题 下一题