1.3.22

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

解答

在 x 之后插入 t,如下图所示。

代码

using System;
using Generics;

namespace _1._3._22
{
    /*
     * 1.3.22
     * 
     * 假设 x 是一条链表中的某个结点,下面这段代码做了什么?
     * 
     */
    class Program
    {
        //将 t 插入到 x 之后
        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";

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

            Console.WriteLine();

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

另请参阅

Generics 库

上一题 下一题