0

我正在用 Java 制作队列的链接实现。问题是,当我将它与其他所需的 .java 文件(队列、QueueDriver 等)一起放入一个包(我正在使用默认包)时,它只会显示错误。它在包装外工作正常。我正在使用 Eclipse。

    public class LinkedQueue<E> implements Queue<E>{

/**
 * Initialize the pointers
 */
private Node<E> front;
private Node<E> rear;

/**
 * Initialize size of list
 */
private int size;

/**
 * Initialize the linked queue
 */
public LinkedQueue() {
    front = null;
    rear = null;
    size = 0;
}

/* (non-Javadoc)
 * @see Queue#isEmpty()
 */
@Override
public boolean isEmpty() {      
    return (front == null);
}

/* (non-Javadoc)
 * @see Queue#dequeue()
 */
@Override
public E dequeue() throws QueueEmptyException {
    if (isEmpty())
        throw new QueueEmptyException("Queue is empty!");
    else{
        E temp = (E) front.getElement();
        front = front.getNext();
        size--;
        return temp;
    }
}

/* (non-Javadoc)
 * @see Queue#enqueue()
 */
@Override
public void enqueue(E element) {
    Node<E> x = new Node<E>(element);
    if(front == null){
        front = rear = x;
    }
    else{
        rear.setNext(x);
        rear = x;
    }
    size++;
}

/* (non-Javadoc)
 * @see Queue#size()
 */
@Override
public int size() {     
    return size;
}

/* (non-Javadoc)
 * @see Queue#front()
 */
@Override
public E front() throws QueueEmptyException {
    if (isEmpty())
        throw new QueueEmptyException("Queue is empty!");

    return (E) front.getElement();
}

}

4

1 回答 1

0

该消息告诉您这Node不是通用的。它需要像

public class Node<E> {
    // your code here, using E as the data in the node
}
于 2013-07-18T05:41:57.727 回答