无法理解此队列的添加方法[重复]

时间:2022-04-26 18:32:47

This question already has an answer here:

这个问题在这里已有答案:

I've found this example on a power point that describes the queue using Java. The whole code is good until I get the Add method which adds new Node or new value to the queue, here is the add method code:

我在使用Java描述队列的power point上找到了这个例子。整个代码都很好,直到我得到Add方法,它为队列添加新的Node或新值,这里是add方法代码:

 public void add(String value ){
        Node node = new Node(value, null);
        if(isEmpty())
            front = rear = node;
        else {
            //I don't understand these two lines
            rear.next = node; //
            rear = node; //
        }
    }

I can't understand the above 2 lines rear.next=node and rear =node, whey we assign next and current to the same object of Node?
Here is code of Class Node:

我无法理解上面的2行rear.next = node和rear = node,乳清我们将next和current分配给Node的同一个对象?这是类节点的代码:

 class Node {
        String value;
        Node next;

        public Node(String value, Node next) {
            this.value = value;
            this.next = next;
        }
        public Node(String value) {
            this.value = value;
        }
    }

1 个解决方案

#1


1  

Append the node to the end of the queue. Or in other words, the node after the end should be the new node:

将节点附加到队列的末尾。或者换句话说,结束后的节点应该是新节点:

rear.next = node;

Now that we have appended a node to the end, the last node is different, not what it was before we appended. The new last node is the node we just added:

现在我们已经将一个节点附加到末尾,最后一个节点是不同的,而不是我们追加之前的节点。新的最后一个节点是我们刚刚添加的节点:

rear = node;

#1


1  

Append the node to the end of the queue. Or in other words, the node after the end should be the new node:

将节点附加到队列的末尾。或者换句话说,结束后的节点应该是新节点:

rear.next = node;

Now that we have appended a node to the end, the last node is different, not what it was before we appended. The new last node is the node we just added:

现在我们已经将一个节点附加到末尾,最后一个节点是不同的,而不是我们追加之前的节点。新的最后一个节点是我们刚刚添加的节点:

rear = node;