Double boolean list logic

I am trying to run a test for the getPrevious () method in JUnit.

public void getPrev(){
    for (int i = 0; i < 1000; i++) {
        list.add(i);    
    }
    list.reset();
    for (int i = 999; i >= 0; i--) {
        int info = list.getPrevious();
        assertEquals(i, info);
    }
}

      

Every other method seems to work except for this one. After doing some printing tests, I realized that the reset method

...reset(){
    if (list != null)
        location = list.getPrev();//returns the last node previous node -- head node.
}

      

(which should make the location of the node head node) will not return the correct information. It returns null instead of head node.

So my logic led me to believe that the add method was not working the way it should. And this is also my question. I'm trying a few things to see where the error is, but nothing seems to work. I am looking to see if anyone can help detect a boolean error in this code.

 public void add(Object elem) {
     LLNode<T> newNode = new LLNode(elem);

     if(list == null){
         tail = list = newNode;
     }
     list.setPrev(newNode);
     newNode.setNext(list);
     newNode.setPrev(tail);
     tail.setNext(newNode);
     list = newNode;
     size++;
 }

      

+3


source to share


1 answer


try it



 public int add(Object elem) {
    Node node = new Node(elem);

    if (head == null) {
        head = node;
    } else {
        tail.setNext(node);
        node.setPrevious(tail);
    }

    tail = node;
    return value;
}

      

+3


source







All Articles