java - 这个双端队列实现有什么问题

标签 java deque

我想写双端队列的实现 我在下面编写了代码,但不幸的是我遇到了问题 当我使用 addFirst 和 addLast 甚至 removeFirst 时一切正常 但是当我使用 removelast 时,程序会抛出 NullPointerException 我不知道到底是什么问题所以我真的很困惑 谁能帮帮我?? 预先感谢您的关注

节点类::

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;
}
}

这里是双端队列接口(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();
}

最后是实现 DQ 类的 MyDQ 类::

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;

}

}

最佳答案

我创建了一个简单的 Person 对象来使用:

public class Person {

    String name;
    Integer age;


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

}

我使用以下单元测试对此进行了测试:

    @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();

    }

这会在这一行(第 78 行)抛出一个空指针:

tail.setNext(null);

至此,调用中的 tail 已通过此行设置为 null:

tail = tail.getPrev();

为了纠正这个问题,我在此时重写了空检查尾部的实现:

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;

} 

关于java - 这个双端队列实现有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20911277/

相关文章:

java - 在java eclipse中读取文本文件作为二维数组错误

java - 使用 ActiveMQ 调度程序的性能下降问题

python - 为什么使用可迭代对象中的最后一个 maxlen 项初始化 python 双端队列?

c++ - 使用 std::deque<std::string> 时出错

python - 集合双端队列构造函数

java - 如何在 6x6 数独中检查 3x2 矩形框;

java - Tomcat 服务器上的 JAX-WS

java - 发送电子邮件时出现 Spring 错误

python - 值错误: need more than one value to unpack