I have been trying to utilize my previous made singly linked list to create a doubly linked list. So, in the Node class I added a previous node reference and updated the add and insert methods in the List class to accommodate going back and forth within the DoublyLinked list class. add puts the new node after the current node and insert puts the new node before the current node in the List class; this is because I want to add and insert my values in ascending order within the DoublyLinked list class. [This may sound confusing but I will post the code below] When I test my add method [in the DoublyLinked class] I get a null pointer exception.
As I said I have been working on this for a few days, so in that time I have taken in consideration these previous questions:
- How do I change a singly-linked list to a doubly-linked list?
- Singly linked list to doubly linked list
- Null pointer when using doubly linked list
List.java
public class List<T> implements ListInterface<T> {
protected class Node {
private T value = null;
private Node prev = null;
private Node next = null;
public T getValue( ) {
return value;
}
public Node getPrev( ) {
return prev;
}
public Node getNext( ) {
return next;
}
public void setValue( T newValue ) {
value = newValue;
}
public void setPrev( Node prevNode ) {
prev = prevNode;
}
public void setNext( Node nextNode ) {
next = nextNode;
}
}
protected Node head = null;
protected Node cur = null;
protected int size = 0;
// add after cur
@Override
public void add( T value ) {
Node temp = new Node( );
temp.setValue( value );
size++ ;
if (isEmpty( )) {
head = temp;
cur = temp;
} else {
temp.setNext(cur.getNext());
temp.setPrev(cur);
cur.setNext(temp);
cur = temp;
}
}
// insert before cur
@Override
public void insert( T value ) {
Node temp = new Node( );
temp.setValue( value );
size++ ;
if (isEmpty( )) {
head = temp;
cur = temp;
} else if (head == cur) {
head = temp;
head.setNext( cur );
cur.setPrev(head);
cur = head;
} else {
Node prev = head;
while( prev.getNext( ) != cur ) {
prev = prev.getNext( );
}
temp.setNext( prev.getNext( ) );
temp.setPrev(prev);
prev.setNext( temp );
cur = temp;
}
}
}
DoublyLinked.java
public class DoublyLinked<T extends Comparable<T>>
extends List<T> implements ListInterface<T> {
private int size;
private Node tail;
DoublyLinked() {
this.size = 0;
this.tail = null;
}
@Override
public void add(T value) {
size++;
reset();
// getting Null pointer on add when doublinked list is empty
if(isEmpty()) {
super.add(value);
head = cur;
tail = head;
cur = head;
}
else {
try {
while(value.compareTo(get()) > 0 && hasNext()) { // error here
next();
if(value.compareTo(get()) <= 0) {
super.add(value);
// how to set cur to this new node?
}
}
} catch (EmptyListException | EndOfListException e) {}
super.add(value); // add at end of list
tail = cur;
cur = tail;
}
}
@Override
public T get() throws EmptyListException {
return cur.getValue();
}
@Override
public T next() throws EmptyListException, EndOfListException {
if (!hasNext( )) {
throw new EndOfListException( );
}
cur = cur.getNext( );
return cur.getValue( );
}
@Override
public boolean hasNext() {
return((!isEmpty()) && (cur.getNext() != null));
}
@Override
public void reset() {
cur = head;
}
@Override
public boolean isEmpty() {
return size == 0;
}
}
Then I have a basic JUnit test to test the code:
import static org.junit.Assert.*;
import org.junit.Test;
public class DoublyLinkedTest {
@Test
public void testAdd() {
DoublyLinked<Integer> list = new DoublyLinked<Integer>();
list.add(1);
}
}