Java 链表优先队列

Posted

技术标签:

【中文标题】Java 链表优先队列【英文标题】:Java Linked List Priority Queue 【发布时间】:2017-01-03 12:00:14 【问题描述】:

我正在尝试使用链表在 java 中创建优先级队列,但有些东西不起作用。我了解优先级队列的一般功能,但在 java 方面我是一个完整的初学者。我查看了其他示例,但似乎找不到我的问题所在。有什么建议吗?我注意到的一件事是使用类型或其他什么,但我不确定那是什么。

头等舱:

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;
    


二等:

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


第三类:

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;           


测试代码:

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

【问题讨论】:

您想将 PriorityQueue 类型的引用分配给 Node 类型的值,它们不会在一个类层次结构中发生事件。 “某事不工作”请明确。什么不工作? 不要评论所有内容。 Node head - 是的,它是列表的首位。你不必用英文写。注释的目的不是将代码翻译成英文。您应该使用 cmets 来解释您所写内容背后的原因。不要将 size 变量声明为 int n 并在其旁边写注释,而是这样做:int size。它更加干净和明显。通过解释为什么会发生而不是发生什么来发表评论:) 【参考方案1】:

测试类的代码

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

由于类型不匹配,此行不起作用:您无法从 Node 转换为 PriorityQueue。

ClassCastException 的 API 规范:

抛出表明代码已尝试将对象强制转换为它不是实例的子类。例如,以下代码生成 ClassCastException:

在您的示例中,这将起作用;

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

【讨论】:

我看到这是我尝试运行它时的原因,但我不确定如何纠正错误。应该使用不同的类(不是节点)创建 deleteMin() 还是?

以上是关于Java 链表优先队列的主要内容,如果未能解决你的问题,请参考以下文章

如何使用优先队列对链表进行排序

优先级队列(从头开始)和链表

使用优先队列对链表进行排序

基于链表的优先队列

Dijkstra 算法运行时的区别:优先队列与双向链表

优先队列