简体   繁体   中英

Java Linked List Priority Queue

I'm trying to create a priority queue in java with the use of a linked list, but something isn't working. I understand the general functionality of the priority queue, but I'm a complete beginner when it comes to java. I've looked at other examples and can't seem to find what's wrong in mine. Any advice? One thing I've noticed is the use of type or or whatever, but I'm not exactly sure what that is.

First class:

public class Node {     //Node class structure

    int data;   //data contained in Node    
    Node next;  //pointer to Next Node

    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }

}

Second class:

import work.Node;

//basic set-up of a singly linked list
public class SLList{

    Node head;  //head of SLList
    Node tail;  //tail of SLList
    int n;      //number of elements in SLList

}

Third class:

import work.Node;

public class PriorityQueue extends SLList{

//add a new node 
public void add(int x){
    Node y = new Node();
    y.data = x;

    if (tail == null){  //if there is no existing tail, thus an empty list
        tail = y;       //assign tail and head as new node y
        head = y;   
    }
    else if (y.data < head.data){   //if new node y is the smallest element, thus highest priority
        y.next = head;              //assign y's next to be current head of queue
        head = y;                   //reassign head to be actual new head of queue (y)
    }
    else{               //if there is already a tail node
        tail.next = y;  //assign the tail's pointer to the new node
        tail = y;       //reassign tail to actual new tail of queue (y)
    }   
    n++;                //increment the queue's size
}

//delete the minimim (highest priority value) from the queue
public Node deleteMin(){
    if (n == 0){            //if the list is of size 0, and thus empty
        return null;        //do nothing
    }
    else{                           //if there are node(s) in the list
        Node min = head;            //assign min to the head
        head = head.next;           //reassign head as next node,               
        n--;                        //decrement list size
        return min;             //return the minimum/highest priority value
    }       
}

//return the size of the queue
public int size() {
    return n;           
}
}

Tester code:

import work.Node;
import work.SLList;
import work.PriorityQueue;

public class Test {

public static void main(String[] args){
    PriorityQueue PQueue1 = new PriorityQueue();

    PQueue1.add(3);
    PQueue1.add(2);
    PQueue1.add(8);
    PQueue1.add(4);

    System.out.println("Test add(x): " + PQueue1);
    System.out.println("Test size() " + PQueue1.size());

    PriorityQueue PQueue2 = new PriorityQueue();
    PQueue2 = PQueue1.deleteMin();                //the data types don't line up but I don't know what should be changed

    System.out.println("Test deleteMin() " + PQueue2);
    System.out.println("Test size() " + PQueue2.size());
}   
}

Code from your Test class

PQueue2 = PQueue1.deleteMin();                //this line isn't working*

This line is not working because of Type mismatch: you cannot convert from Node to PriorityQueue.

API Specifications for the ClassCastException :

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

In your example, this will work;

Node node = PQueue1.deleteMin();              //this will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM