【问题标题】:Difference between PriorityQueue and Queue Implementation in JavaJava中PriorityQueue和队列实现的区别
【发布时间】:2017-11-04 15:20:47
【问题描述】:

我正在通过这个接口实现一个基于数组的priorityQueue:

public interface IPriorityQueue {

/**
 * Adds an element to the queue.
 *
 * @param element the element to be queued
 * @throws QueueFullException if there is no room in the queue for the new element
 */
void enqueue(Comparable element) throws QueueFullException;

/**
 * Removes the largest element.
 *
 * @return the element removed
 * @throws QueueEmptyException if the queue is empty
 */
Comparable dequeue() throws QueueEmptyException;

/**
 * Returns the number of elements in the queue.
 * @return the number of elements in the queue
 */
int size();

/**
 * Checks whether the queue is empty.
 * @return true if the queue is empty
 */
boolean isEmpty();

/**
 * Removes all elements from the queue.
 */
void clear();}

如果我要使用基于数组的通用对象队列,我想知道在实现这些方法时的根本区别是什么。

【问题讨论】:

  • 我不明白你的问题。您还没有发布任何实现。只有一个接口(使用原始类型,这是一个非常糟糕的主意)。所以,你问什么和什么之间的区别?为什么你首先要实现 PriorityQueue? Java 有一个标准的。
  • 这是一个通过谷歌搜索很容易找到的问题:队列是“先进先出”的,而优先级队列由提供诸如堆或树之类的顺序的数据结构支持。投票结束这个问题过于广泛。
  • 我没有正确理解这个问题。你能解释一下吗?

标签: java interface queue priority-queue comparable


【解决方案1】:

据我了解,我相信您是在询问队列和优先级队列之间的区别。

队列:

队列被定义为遵循严格的插入和删除方法(先进先出)的数据结构。

优先队列:

最小优先级队列:它的根中有最小元素。

最大优先级队列:它的根中有最大元素。

优先级队列是一种数据结构,它具有某种顺序的元素,您可以执行以下操作:Extract-Min()、Decrease-Key()、Insert()、deleteMax(),这是因为优先级队列可以比较事物(在一般情况下,它实现了可比性)

PriorityQueue 中的插入和提取最小可以按任何顺序进行,并且取决于优先级队列的实现方式。堆是实现优先级队列的最“有效”方式。但是优先队列的实现性质取决于您的要求。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多