Inserting Node in a Sorted linked list

风格不统一 提交于 2019-12-11 00:12:54

问题


The following code ensures that elements are inserted in a linked list in a sorted manner.

After understanding the logic behind this i decided to test it on my own. However when i wrote my version of the code it as follows.

  public class SortedList {

    private Node first;

    public SortedList() {
        first = null;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public void insert(int j) {
        Node newNode = new Node(j);
        Node previous = null;
        Node current = first;

        while (current != null && j > current.iData) {
            previous = current;
            current = current.next;
        }

        if (previous == null)
            first = newNode;

        else

            newNode.next = current;
        previous.next = newNode;

    }

    public Node remove() {
        Node temp = first;
        first = first.next;
        return temp;
    }

    public void displayList() {
        System.out.println("First to -----> Last");
        Node current = first;

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

}

NODE CLASS

public class Node {

    public int iData;
    public Node next;

    public Node(int id) {
        iData = id;
    }

    public void display() {
        System.out.println(iData + " ");
    }

}

TEST CLASS

public class SortedListApp {

    public static void main(String[] args) {

        SortedList list = new SortedList();

        list.insert(20);
        list.insert(40);

        list.displayList();

        list.insert(10);
        list.insert(30);
        list.insert(50);

        list.displayList();

        list.remove();

        list.displayList();

    }

}

The only difference between the two is that in my version when the while loop terminates. I first set the next value of the newNode to the current and then i set the next value of previous to the new Node. In the original code posted they have it reversed. For some reason this throws Null Pointer Exception. I am wondering why?

From what i understand, once the new node finds its place for insertion. We have reference to the previous node and the current node and we are trying to insert the new node in the middle of the previous and the current. Therefore what i do is set the next of the new Node to the current and then i set the next of previous node to the new node.

Please tell me where i am wrong.


回答1:


Trace through this code when previous is null:

if (previous == null)
    first = newNode;
else
    newNode.next = current;
previous.next = newNode;

Notice that you never actually changed previous in the if statement, so this will try to write the next field of null, causing the crash.

If previous is null, then you need to prepend the node to the list, and in that case all you need to do is set first to newNode and make newNode's next pointer point to the old list. In the case where previous isn't null, then you need to do two rewirings: you need to make previous.next point to the new node and for the new node's next pointer to point to the current node. You can fix this by writing

if (previous == null) {
    newNode.next = current;
    first = newNode;
}
else {
    newNode.next = current;
    previous.next = newNode;
}

Equivalently:

newNode.next = current;
if (previous == null)
    first = newNode;
else
    previous.next = newNode;

Hope this helps!



来源:https://stackoverflow.com/questions/25293668/inserting-node-in-a-sorted-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!