Search code examples
javaclassadtdoubly-linked-list

Why am I getting an error in this Doubly Linked List?


So I've been working on doubly linked lists and I want to insert a value at the head of the list. We should have 2 cases:

  1. Case of an empty list.
  2. Case of a non-empty list.

I think that what I'm doing is correct but still I am having an issue with the result.

public class DoublyLinkedList {
    
    class Element{
        int data ;
        Element next = null; //reference the next element
        Element previous = null; //reference to the previous element
        
        Element(int value){
            data = value;
            next = null;
            previous = null;
        }
    }
    
    
    private Element head = null; //reference to the head of the list
    private Element rear = null; //reference to the rear of the list
    private int length = 0;
    
    public static final int NOT_FOUND = -1; 
    
    //getter return the number of items in the list
    public int getLength() {
        return length;
    }
    
    
    public DoublyLinkedList() {
        this.head = null;
        this.rear = null;
        this.length = 0;
    }
    
    public DoublyLinkedList(DoublyLinkedList dList) {
        this();
        
        if(dList.isEmpty())
            return;
        Element cur = dList.head;
        Element tmp = new Element(cur.data);
        head = rear = tmp;
        cur = cur.next;
        length++;
        while(cur != null) {
            tmp = new Element(cur.data);
            rear.next = tmp;
            tmp.previous = rear;
            rear = tmp;
            cur = cur.next;
            length++;
        }
    }
    
    public boolean isEmpty() {
        return length == 0;
    }

public String toString() {
        Element cur = this.head;
        String str;
        
        if (isEmpty())
            str = "The list is empty";
        else {
            str = "|";
            while (cur != null) {
                str += cur.data + "|";
                cur = cur.next;
            }
            System.out.println();
        }
        return str;
    }

public void insertAtHead(int value) {
        Element tmp = new Element(value);
        
        // case on an empty list
        if(this.isEmpty()) {
            head = rear = tmp;
            head.previous = null;
            rear.next = null;
        }else {
        
        //case of a non-empty list
            tmp.next = head;
            head.previous = tmp;
            tmp.previous = null;
            head = tmp;
            this.length++;
            }
        }

public static void main(String[] args) {
        DoublyLinkedList list = new DoublyLinkedList();
        
        list.insertAtHead(1);
        list.insertAtHead(2);
        list.insertAtHead(3);
        list.insertAtHead(4);
        list.insertAtHead(5);
        System.out.println(list);
        
        list.insertBetween2Nodes(3);
        System.out.println(list);
    }
    
    
}

And it's always giving me this result:

The list is empty.


Solution

  • In your insertAtHead function you aren't incrementing your list length when the list is empty. It should be:

        public void insertAtHead(int value) {
            Element tmp = new Element(value);
            
            // case on an empty list
            if(this.isEmpty()) {
                head = rear = tmp;
                head.previous = null;
                rear.next = null;
            } else {
            //case of a non-empty list
                tmp.next = head;
                head.previous = tmp;
                tmp.previous = null;
                head = tmp;            
            }
            // You always need to increment length
            this.length++;
        }