嗨,我正在尝试使用循环双链表为双端队列执行实验室实现/代码问题是它在我的deQueueRear()
中抛出了ClassCastException
,因为我不知道我做错了什么,并且我实现了该方法的课本算法QueueDoubleEnded::包lab10_mudhi;
/**
*
* @author Moudhi
*/
public class QueueDoubleEnded<E> {
//Nested Node inner-class:
private static class Node<E> {
private E data;
private Node<E> next;
private Node<E> prev;
Node(E data, Node<E> next, Node<E> prev) {
this.data = data;
this.next = next;
this.prev = prev;
}//end of Node constructor
E getData() { return data; }
Node<E> getNext() { return next; }
Node<E> getPrev() { return prev; }
void setData(E data) { this.data = data; }
void setNext(Node<E> next) { this.next = next; }
void setPrev(Node<E> prev) { this.prev = prev; }
public String toString(){return data+"";}
}//end of Node class
//Attributes
private Node<E> front=null, rear=null;
private int size=0;
public QueueDoubleEnded() { }
public boolean isEmpty(){return size==0;}
public int size() {return size;}
public Node<E> getFront() {
if (isEmpty()) return null;
return front;
}
public Node<E> getRear() {
if (isEmpty()) return null;
return rear;
}//end of getRear
public void display(){
if (isEmpty()) {
System.out.println("nERROR: Empty Queue!");
return;
}//end if
System.out.print("n********** Display Queue **************n");
Node<E> current=front;
do{
System.out.println(current.data);
current=current.next;
}while(current!=front);
System.out.println("***************************************n");
}//end of display
public void enQueueRear(E value){ //like addLast in a CDLL
Node<E> newNode = new Node(value, null, null);
if (isEmpty()){
front = rear = newNode;
newNode.next=front;
newNode.prev=front;
}
else{
newNode.prev=rear;
rear.next=newNode;
front.prev=newNode;
newNode.next=front;
rear = newNode;
}//end if-else
size++;
System.out.println(value + " - Added to Queue (enQueueRear)");
}//end of enQueueRear
public Node<E> deQueueFront() {
if (isEmpty()) {
System.out.println("nERROR: Queue Underflow!");
return null;
}//end if
Node<E> temp=front;
front = front.next;
rear.next=front;
front.prev=rear;
size--;
if (size == 0) { rear =front = null; }
System.out.println(temp.data + "- Deleted from Queue(deQueueFront)");
return temp;
}//end of deQueueFront
//Lab 10 Tasks:
public void enQueueFront(E value){
System.out.println(value+" - Added to Queue (enQueueFront)");
Node newNode=new Node(value,null,null);
if (isEmpty()){
front=newNode;
rear=newNode;
}else{
front.prev=newNode;
newNode.next=front;
front=newNode;
}
size++;
}
public Node<E> deQueueRear() {
if (isEmpty()){
System.out.println(" Queue is underflow ");
return null;
}
Node<E> temp = (Node<E>) rear.data;//throws the class cast exception here
rear=rear.prev;
rear.next=null;
size--;
if(size==0) front=null;
return temp;
}
}
主要类别::
package lab10_moudhi;
/**
*
* @author Moudhi
*/
public class Lab10_moudhi {
public static void main(String[] args) {
System.out.println("Testing a Doubly Ended Queue using CDLL:");
System.out.println("--------------------------------------------");
QueueDoubleEnded q2= new QueueDoubleEnded();
q2.enQueueFront(10);
q2.enQueueFront(20);
q2.enQueueFront(30);
q2.enQueueFront(40);
q2.enQueueRear(50);
q2.enQueueRear(60);
q2.deQueueFront();
q2.deQueueRear();//throws error at this call for this method
System.out.println("Front Element:"+q2.getFront());
System.out.println("Rear Element:"+q2.getRear());
q2.display();
System.out.println("--------------------------------------------");
}
}
错误消息::
run:
Testing a Doubly Ended Queue using CDLL:
--------------------------------------------
10 - Added to Queue (enQueueFront)
20 - Added to Queue (enQueueFront)
30 - Added to Queue (enQueueFront)
40 - Added to Queue (enQueueFront)
50 - Added to Queue (enQueueRear)
60 - Added to Queue (enQueueRear)
40- Deleted from Queue(deQueueFront)
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to lab10_moudhi.QueueDoubleEnded$Node
at lab10_moudhi.QueueDoubleEnded.deQueueRear(QueueDoubleEnded.java:120)
at lab10_moudhi.Lab10_moudhi.main(Lab10_moudhi.java:33)
C:UsersMoudhiAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
您正在将一个整数强制转换为任何类型E的节点。如果您想创建一个节点,您需要执行Node<E> = read
。我不确定这是否正确,因为代码没有正确缩进。
如果数据是任何类型的对象(而不是像int这样的原始数据类型(,也可以尝试更改toString((方法,以便写入数据
我也不建议写front = rear = newNode;
这样的东西。这很令人困惑。
并且:不要忘记@Override
到String。我认为添加它们是编写代码的唯一好方法。