Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解释此代码如何工作_C#_Algorithm - Fatal编程技术网

C# 解释此代码如何工作

C# 解释此代码如何工作,c#,algorithm,C#,Algorithm,我在这里制作了一个单独的线程,因为我相信如果我写评论,评论不会将线程推到顶部,因此会通知任何已写入该线程的人 此代码由Micheal Buen在以下线程中提供: LinkedList披头士=新LinkedList(); 披头士乐队; LinkedListNode nextBeatles=披头士.AddAfter(披头士.First,“保罗”); nextBeatles=披头士。AddAfter(nextBeatles,乔治); nextBeatles=披头士。AddAfter(nextBeatl

我在这里制作了一个单独的线程,因为我相信如果我写评论,评论不会将线程推到顶部,因此会通知任何已写入该线程的人

此代码由Micheal Buen在以下线程中提供:

LinkedList披头士=新LinkedList();
披头士乐队;
LinkedListNode nextBeatles=披头士.AddAfter(披头士.First,“保罗”);
nextBeatles=披头士。AddAfter(nextBeatles,乔治);
nextBeatles=披头士。AddAfter(nextBeatles,“Ringo”);
nextBeatles=披头士。AddAfter(nextBeatles,乔治);
nextBeatles=披头士。AddAfter(nextBeatles,“Ringo”);
nextBeatles=披头士。AddAfter(nextBeatles,乔治);
nextBeatles=披头士。AddAfter(nextBeatles,“Ringo”);
nextBeatles=披头士。AddAfter(nextBeatles,乔治);
nextBeatles=披头士。AddAfter(nextBeatles,“Ringo”);
nextBeatles=披头士。AddAfter(nextBeatles,乔治);
nextBeatles=披头士。AddAfter(nextBeatles,“Ringo”);
//把1号线换成5号线
LinkedListNode paulsNode=披头士乐队(6);
LinkedListNode recentHindrance=披头士乐队.AddBefore(保尔斯诺德,“Yoko”);
recentHindrance=披头士乐队。AddBefore(recentHindrance,“米米阿姨”);
披头士乐队。AddBefore(最近的《吉姆神父》);
Console.WriteLine(“{0}”,string.Join(“\n”,beatles.ToArray());
Console.ReadLine();
公共静态类助手
{
公共静态LinkedListNode节点(此LinkedList l,int索引)
{
LinkedListNode x=l.第一;
而((索引--)>0)
{
x=x.下一步;
控制台写入(x.Value);
睡眠(10000);
}
返回x;
}
}
我想知道的是,扩展方法实现了什么

在第一个过程中,x=x。下一个意思是我们在看林戈而不是乔治,以此类推。从我调用NodeAt(6)开始,引擎盖下到底发生了什么,代码在做什么?我这样问是因为能够阅读和理解代码非常重要,而无需使用单步执行方法作为辅助(例如,有时在工作中,您会阅读打印文档中的代码)。另外,为什么我们在循环中向后计数,为什么在进入循环体之前有一个括号减去1


感谢

扩展方法只通过LinkedList的
n
元素,它使用列表的第一个元素(l.first)初始化
x
,然后在这个过程中,它将索引递减到向前推进
n
次(look
x=x.Next;
):


扩展方法只通过LinkedList
n
元素,它使用列表的第一个元素(l.first)初始化
x
,然后在这个过程中,它将索引递减到向前移动
n
次(look
x=x.Next;
):


是的,图片+1。但是这个列表实际上是双链接的谢谢这两个,@Henk我把它画成单链接的,因为算法只在一个方向上遍历列表谢谢对逻辑的解释!事后看来,现在似乎很基本是的,图片+1。但是这个列表实际上是双链接的谢谢这两个,@Henk我把它画成单链接的,因为算法只在一个方向上遍历列表谢谢对逻辑的解释!事后看来,现在似乎很基本
        LinkedList<string> beatles = new LinkedList<string>();

        beatles.AddFirst("John");
        LinkedListNode<string> nextBeatles = beatles.AddAfter(beatles.First, "Paul");
        nextBeatles = beatles.AddAfter(nextBeatles, "George");
        nextBeatles = beatles.AddAfter(nextBeatles, "Ringo");
        nextBeatles = beatles.AddAfter(nextBeatles, "George");
        nextBeatles = beatles.AddAfter(nextBeatles, "Ringo");

        nextBeatles = beatles.AddAfter(nextBeatles, "George");
        nextBeatles = beatles.AddAfter(nextBeatles, "Ringo");

        nextBeatles = beatles.AddAfter(nextBeatles, "George");
        nextBeatles = beatles.AddAfter(nextBeatles, "Ringo");

        nextBeatles = beatles.AddAfter(nextBeatles, "George");
        nextBeatles = beatles.AddAfter(nextBeatles, "Ringo");


        // change the 1 to your 5th line
        LinkedListNode<string> paulsNode = beatles.NodeAt(6);
        LinkedListNode<string> recentHindrance = beatles.AddBefore(paulsNode, "Yoko");
        recentHindrance = beatles.AddBefore(recentHindrance, "Aunt Mimi");
        beatles.AddBefore(recentHindrance, "Father Jim");


        Console.WriteLine("{0}", string.Join("\n", beatles.ToArray()));

        Console.ReadLine();

public static class Helper
{
    public static LinkedListNode<T> NodeAt<T>(this LinkedList<T> l, int index)
    {
        LinkedListNode<T> x = l.First;

        while ((index--) > 0)
        {
                x = x.Next;
            Console.Write(x.Value);
            Thread.Sleep(10000);
        }



        return x;
    }
}
Index: 1 2 3 4 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | John |-> | Paul |-> | George |-> | Ringo | ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾ ‾
var linkedList = new LinkedList<string>(new []{"John", "Paul", "George", "Ringo"});
linkedList.ElementAt(3); // this returns you Ringo, note that 
                         // the index is 0-based!