Search code examples
javasortedlist

How does insert and delete method of SortedList in Java work


This is more about my lack of ability to understand the SortedList Class. The ListNode I understand: it is a node made up of 2 things: A comparable object and a ListNode object which itself has the same 2 things.

I do not understand how the insert and delete method work in the SortedList Class down below. It's very complex to understand. In the SortedList Class, if the head which is null, points to curr. Shouldn't curr be null? Then the while loop should not execute, but it in fact does execute when I use println to test execution.

Code for ListNode Class:

public class ListNode {

    private Comparable data;
    private ListNode next;

    public ListNode() {
        data = null;
        next = null;
    }

    public ListNode(Comparable x) {
        data = x;
        next = null;
    }

    public ListNode(Comparable x, ListNode nextListNode) {
        data = x;
        next = nextListNode;
    }

    public Comparable getData() {
        return data;
    }

    public ListNode getNext() {
        return next;
    }

    public void setData(Comparable x) {
        data = x;
    }

    public void setNext(ListNode nextListNode) {
        next = nextListNode;
    }

}

Code for SortedList:

public class SortedList {

    private ListNode head;

    public SortedList() {
        head = null;
    }

    public void insert(Comparable x) {
        ListNode curr = head, prev = null;
        while (curr != null && x.compareTo(curr.getData()) > 0) {
            prev = curr;
            curr = curr.getNext();
        }
        if (prev == null) {
            head = new ListNode(x, curr);
        } else {
            prev.setNext(new ListNode(x, curr));
        }
    }

    public boolean delete(Comparable x) {
        if (head == null) {
            return false;
        }
        ListNode curr = head, prev = null;
        while (curr != null && x.compareTo(curr.getData()) > 0) {
            prev = curr;
            curr = curr.getNext();
        }
        if (curr == null || x.compareTo(curr.getData()) != 0) {
            return false;
        }
        if (prev == null) {
            head = curr.getNext();
        } else {
            prev.setNext(curr.getNext());
        }
        return true;
    }

    public void output() {
        for (ListNode curr = head; curr != null; curr = curr.getNext()) {
            System.out.println(curr.getData());
        }
    }

}

Code for TestSortedList:

public class TestSortedList {

    public static void main(String[] args) {
        SortedList list = new SortedList();
        list.insert(5);
        list.insert(1);
        list.insert(10);
        list.insert(3);
        list.insert(7);
        list.output();
        System.out.println(list.delete(5));
        System.out.println(list.delete(5));
        System.out.println(list.delete(1));
        System.out.println(list.delete(10));
        System.out.println(list.delete(11));
    }

}

Solution

  • curr is null when the first call to insert is made. So while gets ignored for this time.

    But as prev == null evaluates to true, if block gets executed, essentially executing this line:

    head = new ListNode(x, curr);
    

    With that, head no more remains null, and any further calls to insert may result in while loop getting executed. Whether the while loop will be executed depends on this expression:

    x.compareTo(curr.getData()) > 0
    

    If true, while gets executed, else not.