Search code examples
javadata-structuressortedlistcircular-list

Sorted circular linked list not getting updated after the removal of element?


I am implementing a sorted circular linked list by first populating the list with the sorted elements and also implemented insert and delete functionality to it. However on calling delete it is not updating the list. I tried debugging the code in delete method but no success yet. below is the code snippet of my program.

class CNode {
    public int data;
    public CNode next;

    public CNode() {
        this.data = 0;
        this.next = null;
    }

    public CNode(int data, CNode next) {
        this.data = data;
        this.next = next;
    }

    public CNode(int data) {
        this.data = data;
        this.next = null;
    }
}

AND the driver class -

public class SortedCLL {
    public static CNode head = new CNode();
    public static CNode last = new CNode();
    public static int NoN;

    public SortedCLL() {
        int N = 3;
        int val[] = {4, 2, 6};
        Arrays.sort(val);
        CNode first = new CNode(val[0]);
        head.next = first;
        last.next = first;
        NoN++;

        for (int i = 1; i < N; i++) {
            CNode n = new CNode(val[i]);
            last.next.next = n;
            last.next = n;
            n.next = head.next;
            NoN++;
        }

        //DELETING AN ELEMENT
        delete(2);

        //INSERTING AN ELEMENT
        insert(7);

        CNode check = head.next;
        for (int i = 0; i < NoN; i++) {
            System.out.print(check.data + " ");
            check = check.next;
        }

    }

    public static void main(String args[]) throws Exception {
        new SortedCLL();
    }

    private void insert(int element) {
        CNode n = new CNode(element);
        if(element < head.next.data) {
            n.next = head.next;
            head.next = n;
            last.next.next = n;
            NoN++;
            return;
        }
        int nodes = NoN - 1;
        CNode iter =  head;
        while(nodes-- > 0){
            if(iter.data < element && iter.next.data > element) {
                n.next = iter.next;
                iter.next = n;
                NoN++;
                return;
            }
        }
        last.next.next = n;
        last.next = n;
        n.next = head.next;
        NoN++;
        return;
    }

    private void delete(int element) {
        //System.out.println( " :  " +element);
        CNode prev = last.next;
        CNode iter =  head.next;
        int nodes = NoN;
        while(nodes-- > 0) {
            if(iter.data == element) {
                //HERE IT IS PRINTING CORRECT PREV AND NEXT NODE'S DATA.
                System.out.println( prev.data + " :  " +iter.next.data);
                prev.next = iter.next;
                NoN--;
                return;
            }
            prev = iter;
            iter = iter.next;
        }
        return;
    }

}

Check the debug statement in the SortedCLL class, in delete method, it is printing correct values of prev and next, still the output is not as expected.

expected list

4 6 7

program's list

2 4 6 7

Any help appreciated !


Solution

  • You are forgetting the edge case of removing the head of the list that requires doing head = head.next; on removal.

    Also you're not really handling the case of an empty list anywhere so watch out for that!