Search code examples
javalinked-listdoubly-linked-listcircular-list

How do I remove an element from a doubly circular linked list properly?


I have to implement a doubly circular linked list using my own constructor, I am pretty much done but can not figure out why the remove method is not working.

I have done plenty of research but I have had difficulties finding anything that matches my needs. The problem is that I do not have a permanent head and tail pointer, like usually in a doubly linked list but must work with the "header" as the starting as well as the ending point.

Constructor with the header element

public MyDoubleLinkedList() {
        header = new DEntry(0, null, null);
        header.next = header;
        header.previous = header;
        size = 0;
    }

Inner class for the listEntrys

    class DEntry {
        /** the data element represented by this entry */
        private final int data;

        /** reference to the previous element in the list */
        private DEntry previous;

        /** reference to the next element in the list */
        private DEntry next;

        /**
         * @param data     the data object this entry represents
         * @param previous reference to the previous element in the list
         * @param next     reference to the next element in the list
         */
        public DEntry(int data, DEntry previous, DEntry next) {
            this.data = data;
            this.previous = previous;
            this.next = next;
        }
    }

Method to add to the list:

    /**
     * Adds a new element into the list at the position specified
     * 
     * @param position the 0 based position at which to add the passed value
     * @param value    the value to add
     * @return 0 if adding was successful, -1 if not
     */
    public int add(int position, int value) {
        // TODO: please put your code here
        DEntry listEntry = new DEntry(value, null, null);

        DEntry temp = header;
        int i = 0;

        if (position < 0 || position > size) {
            return -1;
        }

        if (position == 0) {
            temp = header;

        } else {
            while (i < position) {
                temp = temp.next;
                i++;
            }
        }

        listEntry.next = temp.next;
        listEntry.previous = temp.next;
        temp.next = listEntry;
        temp.next.previous = listEntry.next;
        size++;

        return 0;
    }

Method to remove from the list

    /**
     * Removes an element at the position specified from the list
     * 
     * @param position the 0 based position of the value to remove
     * @return value of the removed entry if removing was successful, -1 if not
     */
    public int remove(int position) {
        // TODO: please put your code here

    DEntry toBeDeleted = header;

        if(position < 0 || position > size) {
            return -1;
        }
        if(getEntry(position) == null) {
            return -1;
        } else {
        toBeDeleted = getEntry(position);
        }

        int dataOfDeletedNode = toBeDeleted.data;

        if(position == 0) {
            header.previous.next = toBeDeleted.next;
            header.next.previous = toBeDeleted.previous;
        } else if(position == size){
            toBeDeleted.previous.next = header.next;
            toBeDeleted.next.previous = toBeDeleted.previous;
        } else {
            toBeDeleted.previous.next = toBeDeleted.next;
            toBeDeleted.next.previous = toBeDeleted.previous;
        }



        size--;
        System.out.println(dataOfDeletedNode);
        return dataOfDeletedNode;
    }

If I run the code

list.add(0, 10);
list.add(1, 20);
list.add(0, 30);
remove(1); // 10 should be deleted

Instead of 30, 20 I get just 20.


Solution

  • I could solve this problem in the meantime. It was indeed my add method that prevented my remove method to work properly.

    Partially at fault was my while loop which stopped at (i

    Here is what I came up with on the add method, and everything is workin fine.

    public int add(int position, int value) {
    
        // Creates a new listEntry 
        DEntry listEntry = new DEntry(value, null, null);
        DEntry temp = header;
    
        int i = 0;
    
        if (position < 0 || position > size) {
            return -1;
        }
    
            while (i <= position) {
                temp = temp.next;
                i++;
            }
    
        // setting the elements neighbours
        listEntry.next = temp;
        listEntry.previous = temp.previous;
    
        // placing the new element between last and next
        temp.previous.next = listEntry;
        temp.previous = listEntry;
    
        // places the new entry in the list
        temp = listEntry;
        size++;
    
        return 0;
    }