1

Hi I'm trying to do my lab implementation/code for Doubly Ended Queue using circular double linked list the problem is that it throws ClassCastException in my deQueueRear() as i don't know what did i do wrong and i implemented the text books algothm for that method the QueueDoubleEnded:: package lab10_moudhi;

/**
 *
 * @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;
}

 }

The main class::

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("--------------------------------------------");   
    }

}

The error message ::

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:\Users\Moudhi\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

1 Answer 1

0

You are casting an integer to a node of any type E. If you want to create a Node you need to do Node<E> = read. I am not sure if this is correct, the code is not indented correctly.

Also try to change your toString() method so that you write data.toString(), if data is any type of object (not primitive data type like int,...).

Also I would not recommend writing things like front = rear = newNode;. This is quite confusing.

And: don't forget to @Override toString. I think adding them is the only good way to write code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.