0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
P_M
  • 42
  • 6
  • 1
    Read [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for tips on debugging your code. – Code-Apprentice Apr 16 '21 at 20:30
  • 1
    Java or Javascript? That is the question... – fantaghirocco Apr 16 '21 at 20:40
  • I think I see the error, but for your own education I think you should try to find it yourself. It's fairly simple. From the link above, start with the "rubber duck" solution. It's fairly easy to find if you just walk yourself through the code. If you still can't find it, try writing smaller test cases. You are currently trying to insert five elements in one go. Try inserting just one element and look at the result in a debugger. Pay attention to the concept of pre-conditions and post-conditions. There's an important one you are missing. – markspace Apr 16 '21 at 20:42
  • Where is the method ``isEmpty()`` defined? – NomadMaker Apr 17 '21 at 10:32
  • Why do you have a ``println()`` call in ``toString()``? – NomadMaker Apr 17 '21 at 10:34
  • @NomadMaker after the copy constructor – P_M Apr 17 '21 at 21:31

1 Answers1

0

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++;
    }
Mady Daby
  • 1,271
  • 6
  • 18