在一个LinkedList的末尾添加一个元素。

huangapple go评论57阅读模式
英文:

Add an element at the end of a LinkedList<T>

问题

I have a code which adds a node at the end of a LinkedList<T>, but I don't understand how it works: when the code exits the "else" statement, the top node contains the previous elements and appends the pointer elements at the end of the list.

I don't know when the pointer elements are added to the top node. Why does this happen, and how does it work?

英文:

I have a code which add a node at the end of a LinkedList<T> but I don't understand how it works: when the code ends else statement the top node has the previous elements and add the pointer elements at the final of the list.

I don't know when the pointer elements are added at top node. Why does this happen and how does it works?

//Link element as latest of the List
public void addAtTheEnd(T element) {
    Node&lt;T&gt; aux = new Node&lt;&gt;(element);
    
    if(this.isEmpty()) {
        top = aux;
    } else {
        Node&lt;T&gt; pointer = top;            
        while (pointer.getNext() != null) {
            pointer = pointer.getNext();
        }
        pointer.setNext(aux);
    }
    size++;
}

在一个LinkedList<T>的末尾添加一个元素。

在一个LinkedList<T>的末尾添加一个元素。

答案1

得分: 2

Sure, here's the translated content:

简单来说:

  1. 如果 块(您的列表为空):节点将作为第一个且唯一的元素添加,因此它是顶部,也是最后

  2. 否则 块(您的列表不为空):您会遍历节点,也就是您会一次又一次地移动到下一个节点,直到达到最后(尾部)一个节点,一旦到达它 - 您会将接收到的参数(节点)添加为当前最后一个节点的下一个节点。

希望这有所帮助。

英文:

It's simple:

  1. if block (your list is empty): Node just gets added as the first and only element, hence it's the top and it's the last;

  2. else block (your list is not empty): You iterate over the nodes, namely you move to the next node, again, and again, until you reach the last (tail) one, and the moment you reach it - you add the received argument(node) as the next node of the currently last one.

I hope this helps.

答案2

得分: 1

  1. 假设您的节点为空,即代码进入IF块,并将aux节点附加到顶部节点。
  2. 现在,如果节点不为空,则进入else块,在那里创建一个以顶部节点初始化的新指针。while循环迭代,直到找到一个值为null的指针。最后,将新的aux节点附加到节点的末尾。
    为了获得正确的可视化想法,您可以观看像mycodeschool这样的YouTube频道。
英文:
  1. Assume your node is Empty i.e. the code goes in the IF block and the aux node gets attached to the top node.
  2. Now if the node is not empty then it goes in the else block where it makes a new Pointer initialized with the top node. The while loop iterates till it finds a pointer which has null value. And finally attaches the new aux node at the end pf the node.
    To get proper visual idea you may watch youtube channels like mycodeschool.

答案3

得分: 0

Great answers but I found this explanation slightly clearer:

  1. 创建一个名为 "aux" 的节点,并将参数 "element" 传递给节点构造函数。
  2. 如果你的列表为空,那么没有其他节点,所以你只需将 "top" 设置为刚刚创建的 "aux" 节点。
  3. 否则,你创建一个指针指向 "top" 节点,并循环直到它没有指向 next 元素的引用。当你达到这一点(也就是 最后一个元素,在这里没有下一个元素),你将它的下一个节点设置为 "aux",将 "aux" 变为最后一个节点。
英文:

Great answers but I found this explanation slightly clearer:

  1. You create an "aux" Node and pass the argument "element" to the Node constructor.
  2. If your list is empty, there's no other nodes, so you simply set "top" as the node "aux" (the Node you've just created).
  3. Else, you create a pointer pointing to the "top" node and loop until it doesn't have a reference to the next element. When you reach that point (aka the last element, where you have no next), you set it's next node to "aux", making aux the last node.

huangapple
  • 本文由 发表于 2020年7月28日 03:01:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63121838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定