简体   繁体   中英

what is wrong with this deque implementation

I want to write implementation of Deque I have written code below but unfortunately I have a problem with it when I use addFirst and addLast or even removeFirst every thing is ok but when I use removelast the programs throws NullPointerException I dont know what is the problem exactly so I got really confused Can Anyone plaese Help me?? Thanks in advance for your attention

Node Class::

public class Node<E>{

E element;
Node<E> prev , next;

public Node(E element, Node<E> prev, Node<E> next) {
    this.element = element;
    this.prev = prev;
    this.next = next;
}

public Node() {
    this(null, null, null);
}

public void setNext(Node next)
{
    this.next = next;
}

public void setPrev(Node prev)
{
    this.prev = prev;
}

public Node getNext()
{
    return next;
}

public Node getPrev()
{
    return prev;
}
}

here is Deque Interface::

public interface DQ<E> {
public int size();
public boolean isEmpty();
public E getFirst();
public E getLast();
public void addFirst (E element);
public void addLast (E element);
public E removeFirst();
public E removeLast();
}

and finally here is MyDQ Class which implements DQ class::

public class MyDQ<E> implements DQ<E>{

Node<E> head , tail;
int size = 0;
@Override
public int size() {
    return size;
}

@Override
public boolean isEmpty() {
    return size == 0;
}

@Override
public E getFirst() {
    if(head == null)
        return null;
    return head.element;

}

@Override
public E getLast() {
    if(head == null)
        return null;
    return tail.element;
}

@Override
public void addFirst(E element) {
    Node<E> n = new Node<>(element, null, null);
    if(head == null)
        head = tail = n;
    else
    {
        head.setPrev(n);
        n.setNext(head);
        head = n;
    }
    size++;
}

@Override
public void addLast(E element) {
     Node<E> n = new Node<>(element, null, null);
    if(head == null)
        head = tail = n;
    else
    {
        tail.setNext(n);
        n.setPrev(head);
        tail = n;
    }
    size++;
}

@Override
public E removeFirst() {
    if(head == null)
        return null;
    Node<E> n = head;
    head = head.getNext();
    head.setPrev(null);
    n.setNext(null);
    size --;
    return n.element;
}

@Override
public E removeLast() {
    if(head == null)
        return null;
    Node<E> n = tail;
    tail = tail.getPrev();
    tail.setNext(null);
    n.setPrev(null);
    size --;
    return n.element;

}

}

I created a simple Person object to use:

public class Person {

    String name;
    Integer age;


    public Person(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }

}

I tested this using the following unit test:

    @Test
    public void testOneElementDeque() {
        MyDQ<Person> deq = new MyDQ<>();
        Person p1 = new Person("John", 12);
        Person p2 = new Person("Eric", 45);

        deq.addLast(p1);

        assertEquals(p1, deq.getLast());
        assertEquals(p1, deq.getFirst());

        deq.removeLast();

    }

This throws a null pointer at this line(line 78):

tail.setNext(null);

By this point in the call tail has been set to null by this line:

tail = tail.getPrev();

To correct this I rewrote the implementaion to null check tail at this point:

public E removeLast() {
    if(tail == null)
        return null;
    Node<E> n = tail;
    tail = tail.getPrev();
    if(tail != null)
        tail.setNext(null);
    n.setPrev(null);
    size --;
    return n.element;

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM