FIFA2000OPEC 2020-01-28 07:41 采纳率: 100%
浏览 167
已采纳

java做链表队列增删节点的时候报错,只列出了链表部分,和主程序部分,刚刚入门java,很多地方不懂,求教

import java.util.NoSuchElementException;

public class ListQueue implements MyQueue {

private class Node {

    T item; 
    Node next; 


    public Node(T item) {
        this.item=item;

    }
}

private Node head = null;
private Node tail = null; // root node
private int size = 0;


public boolean isEmpty() {


    return size == 0;
}


public int size() {


    return size;
}


public void enqueue(T item) {

    Node temptail = new Node(item);
    if (isEmpty()) {
        tail = temptail;
    } else {
        tail.next = temptail;

    }


    size++;

}

/**
 * dequeue: remove and return the head of the queue
 * 
 * @return the deleted value
 * @throws NoSuchElementException if queue is empty
 */
public T dequeue() {

    if (isEmpty()) {
        throw new NoSuchElementException("the list is empty, cannot be taken data");
    }
    else if (size()==1) { // when list just only has one elements
        head = null;
    } else {
        head = head.next;
    }
    size--;


    return null;
}

/**
 * peek: view the head of queue without removing it.
 * 
 * @return Null if queue is empty
 */
public T peek() {
    // if array is empty, cannot view its head
    if (isEmpty()) {
        return null;
    }
    return head.item;
}

}

public class Driver{

public static void main(String[] args) {

    MyQueue<Integer> q;

    boolean useList = true;  // make it false if you want to array implementation

    if (useList)
        q = new ListQueue<Integer>();
    else
        q = new ArrayQueue<Integer>();

    for(int i = 0; i < 1000; i++)  // add a large number of items onto the queue
    {
        q.enqueue(i);  
    }

    System.out.println("now, dequeue items!");
    while(!q.isEmpty())  
    {
        System.out.print(q.dequeue() + " ");  
    }
    System.out.println("end of dequeueing");    



}

}

//运行程序以后错误是now, dequeue items!
Exception in thread "main" java.lang.NullPointerException
at yao.as1.ListQueue.dequeue(ListQueue.java:73)
at yao.as1.Driver.main(Driver.java:24)

还请给点思路
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-01-28 11:33
    关注
    public T dequeue() {
    
        if (isEmpty()) {
            throw new NoSuchElementException("the list is empty, cannot be taken data");
        }
        else if (size()==1) { // when list just only has one elements
            T temp = head; //加上
            head = null;
                    return temp; //加上
        } else {
            T temp = head; //加上
            head = head.next;
                    return temp; //加上
        }
        size--;
    
    
        return null;
    }
    

    这个方法,无论队列是否为空都返回了null,看我的更改

    问题解决的话,请点下采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog