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

时间:2015-07-16 20:40:06

标签: 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.");
    }

}

我需要使用这一个方法来预先对对象进行排序,而不是将它们发送到列表,或者正确地对它们进行排序,一次,在运行中,我的想法已经用完了。 / p>

1 个答案:

答案 0 :(得分:0)

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

这就是概念。对于我们来说,您可能需要提供有关优先级队列应该如何工作的更多详细信息。对于以下注释,我将假设: - 首先检索最高优先级 - 应按插入顺序检索相同的优先级

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

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

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

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
}

然后检索是微不足道的:它只是队列中的第一个项目。

相关问题