LinkedList如何跟踪所有节点-C#? [英] How a LinkedList Keeps Track of All Nodes - C#?

查看:139
本文介绍了LinkedList如何跟踪所有节点-C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力开发自己的单链列表,我不明白如何在链列表的末尾插入节点?

I have been struggling trying to develop my own singly linked list, I cant understand how a node is inserted at the end of a Linked List?

以下是代码:

class LinkedList
{
    private Node head;

    public void AddLast(int value)
    {
        if (head == null)
        {
            head = new Node();

            head.value = value;
            head.next = null;
        }
        else
        {
            Node temp = new Node();
            temp.value = value;

            Node current = head;

            while (current.next != null)
            {
                current = current.next;
            }

            current.next = temp;
        }
    }

    public void PrintAll() 
    {
        Node current = head;

        while (current != null)
        {
            Console.WriteLine(current.value);
            current = current.next;
        }

    }
}

主要方法

    static void Main(string[] args)
    {
        LinkedList list = new LinkedList();
        list.AddLast(3);
        list.AddLast(5);
        list.AddLast(4);
    }

1)我完全得到了第一部分列表。AddLast(3)。由于head为空,我们创建一个新的节点head并为其赋值。

1) I totally get the first part list.AddLast(3). Since head is null, we create a new node head and assign values to it.

2)调用list.AddLast(5)时,head不再为null,因此,我们创建了一个新的临时节点,并为其分配值。现在我们创建一个新的当前节点来保存Head的值,需要注意的是Head.Next为空。

2) When list.AddLast(5) is called, head is no more null, and thus we create a new temporary node, assign values to it. NOW we create a new node current which holds values of Head, it is to be noted Head.Next was null.

现在,我们遍历current并将temp节点放置到current.Next。

Now we iterate through current and place our temp node to current.Next.

3)现在,在调用list.AddLast(5)时,是否应该用Head的内容再次覆盖当前值?分别是Head.Value = 3和Head.Next =空。

3) Now, Upon calling list.AddLast(5) shouldn't the current be again overwritten with the contents of Head? which was Head.Value = 3 and Head.Next = Null.

所以它不应该是current.Value = 3和Current.Next =空吗?如果不是,那为什么呢?

So shouldn't it current.Value = 3 and Current.Next = Null? And if not then why?

推荐答案

Node current = head;

执行以上语句时, current 为暂时仅分配 head 的引用,而不分配值。因此,当时的当前仅指向 head 。如果在此语句后执行, current.value 将为您提供 head 的值。

When the above statement executes, current is assigned only the reference of head temporarily and not the value. So current only points to head at that moment. current.value will give you the value for head if executed after this statement.

while (current.next != null)
{
    current = current.next;
}

现在,当上述同时循环执行,循环访问链表,并将当前节点移到链表中的最后一个节点,该节点的 current.next = null

Now, when the above while loop executes, it iterates through the linked list and takes the current node to the last node in the linked list which will have current.next = null.

current.next = temp;

执行以上语句时,新节点将添加到链接列表的最后。

When the above statement executes, the new node is added to the last of the linked list.

这篇关于LinkedList如何跟踪所有节点-C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆