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

标签 java linked-list priority-queue

我正在尝试从一些无序的原始数据(String1...Priority10、String2...IntPriority2 等)创建一个链接列表,并且在概念化如何排序编写优先级排队的好方法时遇到了困难。我需要获取按顺序对每个对象进行排队的方法,而不是在最终链表上使用排序算法,或使用任何 LinkedList 或 PriorityQueue 本身。

我的入队方法,这里没什么难的:

    public class ObjectQueue{
Object front = null;           //points to first element of the queue
Object prev = null;            //points to last element of the queue 
  /**
 * Creates an object of Object and adds to the class queue
 * @param name of object
 * @param rank of priority sort
 */
public void enQueue(String name, int rank)
{
    Object current = new Object(name, rank);  //uses current entry String as name, int as rank

    if(isEmpty())               //if empty, add element to front
    {
        front = current;
    }
    else                        //if elements exist, go to end and create new element
    {
        prev.next = current;
    }
    prev = current;

我遇到问题的优先级排序和添加方法:

/**
 * Adds each object and rank on a ascending rank basis
 * @param filename name of data file
 * @throws exc in case of missing file
 */
public void addPriority(String filename) throws IOException
{
    try
    {
    File inFile = new File(filename);           //inst. file import
    Scanner read = new Scanner(inFile);         //inst. scanner object

    String name1 = read.next();              //scanner reads next string, primes at front
    int rank1 = read.nextInt();              //reads next int, assigns to rank

    while (read.hasNext())                      //reads until end of text
    {
        String name2 = read.next();              //next string of next Object to be tested
        int rank2 = read.nextInt();              //rank to test rank1 against

        if (rank1 > rank2)                      //if current is higher priority than test
        {
            enQueue(name1, rank1);              //enqueue the current object
            name1 = name2;                      //move test name down to current
            rank1 = rank2;                      //move test rank down to current
        }
        else
        {
            enQueue(name2, rank2);              //enqueue the current object
        }
    }
    read.close();                               //ends read when empty

    }
    catch(Exception exec)
    {
        System.out.println("Error: file not found.");
    }

}

我需要使用这一单一方法来对对象进行预排序而不将它们发送到列表,或者一次在运行中对它们进行正确排序,但我已经没有想法了。

最佳答案

从概念上讲(忽略实现)优先级队列非常简单。它需要能够添加具有优先级的项目,并且需要能够获取具有最高(或者,在某些实现中,最低)优先级的项目。有时包含的附加约束是,对于具有相同优先级的两个项目,必须先检索最先添加的项目。

这就是这个概念。为了让我们为您提供帮助,您可能需要提供一些有关优先级队列究竟如何工作的更多详细信息。对于以下注释,我将假设: - 首先检索最高优先级 - 应按插入顺序检索同等优先级

从实现来看,只要允许插入并保留插入顺序,底层结构就可以是任何集合。传统的实现是堆,因为它们有效地使用内存并且速度非常快,但链表(单链表或双链表)在功能上很好。

显然,优先级队列意味着检索顺序。这可以在插入或检索时实现。同样,这个决定将由使用情况决定,而且您的实现似乎可以忽略这些因素。

因此,为了简单起见,我的建议是在插入时而不是检索时进行排序。在不向您提供实际代码的情况下(我假设这是您的任务),这里有一个可以实现插入时间优先级队列的基本算法。

class PriorityItem {
    private final Item item;
    private final int priority;
}

Collection<PriorityItem> queue;

void insert(Item item, int priority) {
    PriorityItem element = new PriorityItem(item, priority); 
    if queue is not empty {
        step through queue {
            if current.priority < priority {
                insert element here
                return
            }
        }
    }
    add element to end queue
}

然后检索就很简单:它只是队列中的第一项。

关于java - 优先级队列(从头开始)和链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31464109/

相关文章:

java - 在java中引用数组中的值

java - 为什么链表的头属性设置为transient

python - 合并堆的复杂性

c++ priority_queue 初始化。为什么我们可以忽略 const Compare&

java - 编写类似代码行的更聪明的方法

java - 一个类有没有可能实现两个接口(interface),每个接口(interface)都包含一个具有相同方法头的方法?

java - Jackson:如何在 JSON 序列化中包含空值属性

c - 迭代链表

Java:删除链表中的所有元素

java - PriorityQueue 类的标准行为是什么?