我正在用Java制作一个队列的链接实现。问题是,当我把它和其他需要的.java文件(Queue、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();
}
}
消息告诉您Node
不是通用的。它需要像一样
public class Node<E> {
// your code here, using E as the data in the node
}