Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 优先级队列(从头开始)和链表_Java_Linked List_Priority Queue - Fatal编程技术网

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

Java 优先级队列(从头开始)和链表,java,linked-list,priority-queue,Java,Linked List,Priority Queue,我试图从一些无序的原始数据(String1…Priority10、String2…IntPriority2等)创建一个链表,但在概念化如何排序和编写一个好的优先级排队方法时遇到了困难。我需要得到一种方法,使每个对象按顺序排队,而不必在最终的链表上使用排序算法,也不必使用任何LinkedList或PriorityQueue本身 我的排队方法,这里没什么难的: public class ObjectQueue{ Object front = null; //points

我试图从一些无序的原始数据(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
}
类优先项{
私人最终项目;
私有最终int优先级;
}
收集队列;
无效插入(项目,整数优先级){
PriorityItem元素=新的PriorityItem(项目,优先级);
如果队列不是空的{
单步队列{
如果当前优先级<优先级{
在此插入元素
返回
}
}
}
将元素添加到结束队列
}
那么检索就很简单了:它只是队列中的第一项。

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

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

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

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

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

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
}
类优先项{
私人最终项目;
私有最终int优先级;
}
收集队列;
无效插入(项目,整数优先级){
PriorityItem元素=新的PriorityItem(项目,优先级);
如果队列不是空的{
单步队列{
如果当前优先级<优先级{
在此插入元素
返回
}
}
}
将元素添加到结束队列
}

那么检索就很简单了:它只是队列中的第一项。

实现优先级队列不需要排序。您应该阅读有关堆的内容。还有,为什么要使用链表而不是数组?这是非常低效的方法,我需要使用链表,而不是数组。没有LinkedList对象,只有我自己从头开始的对象。然后阅读有关二进制堆的内容,并尝试使用双链接列表()实现它们。实现优先级队列不需要排序。您应该阅读有关堆的内容。还有,为什么要使用链表而不是数组?这是非常低效的方法,我需要使用链表,而不是数组。没有LinkedList对象,只有我自己的从头开始。然后阅读有关二进制堆的内容,并尝试使用双链接列表()实现它们