简体   繁体   English

Java:链接列表节点问题

[英]Java: Linked list node questions

I am learning Linked Lists and came across the following code 我正在学习链接列表并遇到以下代码

public void add(WordMeaning wm)
{

    WordMeaningNode data = new WordMeaningNode(wm);

    if (head == null)
        head = data;

    else
    {
        WordMeaningNode current = head, prev = null;
        boolean found = false;

        while (current != null && !found)
        {
            if (data.getWordMeaning().getWord().compareTo(current.getWordMeaning().getWord()) < 0)
                found = true;

            else
            {
                prev = current;
                current = current.next;         
            }

        }

        data.next = current;

        if (prev == null)
            head = data;

        else
            prev.next = data;


    }

}

This code is used to take a word and definition and then sorts the word alphabetically with the other values in the LinkedList. 此代码用于获取单词和定义,然后使用LinkedList中的其他值按字母顺序对单词进行排序。 I understand how to store words in a LinkedList without being sorted alphabetically but I'm having trouble understanding the last part starting with the data.next = current and the if statement that follows. 我理解如何在不按字母顺序排序的情况下在LinkedList中存储单词,但是我无法理解从data.next = current和后面的if语句开始的最后一部分。 Specifically how data.next and current can be equal to each other as they should both be null. 具体来说,data.next和current可以如何彼此相等,因为它们都应该为null。 I also don't understand why prev.next in the last else statement cannot be substituted for current as they should be the same value based on the previous else statement in the while loop. 我也不明白为什么最后一个else语句中的prev.next不能代替current,因为它们应该是基于while循环中前一个else语句的相同值。

This code inserts word into linked list so that words in resulting linked list are in ascending order. 此代码将单词插入到链接列表中,以便生成链接列表中的单词按升序排列。 data is new node having a word; data是具有单词的新节点; the if condition inside while loop is checking whether word in current node is greater than word in data node. while循环中的if条件是检查current节点中的字是否大于data节点中的字。 If so set found to true else assign current node to previous and current 's next node to current node. 如果是这样的设置found真别的分配current节点以前和current的下一个节点到current节点。 This process repeats until the condition in while evaluates to false. 重复此过程,直到while的条件为false。

There are two cases here: 这里有两种情况:

  1. Once the word in current node is greater than word in data node found becomes true. 一旦在字current节点比字更大的data节点found为真。 Now we know that word in current node is alphabetically greater than word in data node; 现在我们知道current节点中的字在字母上大于data节点中的字; so make current node as next node of data node. 所以将current节点作为data节点的下一个节点。 this is exactly what data.next = current; 这正是data.next = current; is doing. 是在做。 We also know that word in data node is alphabetically greater than word in prev node so make data node as next node of prev node. 我们也知道data节点中的字在字节上大于prev节点中的字,因此将data节点作为prev节点的下一个节点。 This is exactly what prev.next = data; 这正是prev.next = data; of else is doing. else正在做。
  2. This case is when list is empty. 这种情况是列表为空时。 The if (prev == null) head = data; if (prev == null) head = data; makes data node as head node when list is empty. 当列表为空时,将data节点作为head节点。

The simple answer is that they are not equal. 简单的答案是他们不平等。 You forget that in the code data.next = current; 你忘了在代码data.next = current; , it is not asserting that the two are equal. ,并不是说两者是平等的。 It is setting data.next to be current . 它将data.next设置为current

If it still doesn't make sense, think about it this way: 如果它仍然没有意义,那就这样思考:

Node prev is linked to Node current because prev.next == current : 节点prev与节点current链接,因为prev.next == current

  prev
prev.next ------> current
                current.next

However, we want to insert data inbetween the two. 但是,我们希望在两者之间插入data Here's what the list should look like afterwards: 以下是列表之后应该是什么样子:

  prev
prev.next ------> data
                data.next ------> current
                                current.next

Therefore, two things need to happen: 因此,有两件事需要发生:

  • prev.next needs to be set to data . prev.next需要设置为data (We want to change the link and not the thing it links to, so we have to write prev.next , not current .) (我们想要更改链接而不是链接到的内容,因此我们必须编写prev.next ,而不是current 。)
  • data.next needs to be set to current . data.next需要设置为current You are also correct that current could be null, but this does not matter since we are allowed to set things to null. 你也是正确的,当前可能是null,但这并不重要,因为我们被允许将事物设置为null。

It's more concerning if prev is null, since you cannot access prev.next if prev does not exist. 如果prev为null,则更为关注,因为如果prev不存在,则无法访问prev.next That's why this is checked for separately. 这就是为什么要单独检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM