Kodean
Kodean

Reputation: 83

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. 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. Specifically how data.next and current can be equal to each other as they should both be 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.

Upvotes: 1

Views: 212

Answers (2)

NAK
NAK

Reputation: 458

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; the if condition inside while loop is checking whether word in current node is greater than word in data node. If so set found to true else assign current node to previous and current's next node to current node. This process repeats until the condition in while evaluates to false.

There are two cases here:

  1. Once the word in current node is greater than word in data node found becomes true. Now we know that word in current node is alphabetically greater than word in data node; so make current node as next node of data node. this is exactly what 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. This is exactly what prev.next = data; of else is doing.
  2. This case is when list is empty. The if (prev == null) head = data; makes data node as head node when list is empty.

Upvotes: 2

Daniel Causebrook
Daniel Causebrook

Reputation: 479

The simple answer is that they are not equal. You forget that in the code data.next = current;, it is not asserting that the two are equal. It is setting data.next to be 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
prev.next ------> current
                current.next

However, we want to insert data inbetween the two. 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. (We want to change the link and not the thing it links to, so we have to write prev.next, not current.)
  • data.next needs to be set to current. You are also correct that current could be null, but this does not matter since we are allowed to set things to null.

It's more concerning if prev is null, since you cannot access prev.next if prev does not exist. That's why this is checked for separately.

Upvotes: 1

Related Questions