Merge pull request #707 from hailK11/patch-1

Update PriorityQueues.java
This commit is contained in:
Libin Yang
2019-02-17 09:19:26 +08:00
committed by GitHub

View File

@ -1,21 +1,25 @@
/**
* This class implements a PriorityQueue.
*
* <p>
* A priority queue adds elements into positions based on their priority.
* So the most important elements are placed at the front/on the top.
* In this example I give numbers that are bigger, a higher priority.
* Queues in theory have no fixed size but when using an array
* implementation it does.
*
* @author Unknown
*
*/
class PriorityQueue {
/** The max size of the queue */
/**
* The max size of the queue
*/
private int maxSize;
/** The array for the queue */
/**
* The array for the queue
*/
private int[] queueArray;
/** How many items are in the queue */
/**
* How many items are in the queue
*/
private int nItems;
/**
@ -35,10 +39,12 @@ class PriorityQueue{
* @param value Value to be inserted
*/
public void insert(int value) {
if (isFull()) {
throw new RuntimeException("Queue is full");
}
if (nItems == 0) {
queueArray[0] = value;
}
else{
} else {
int j = nItems;
while (j > 0 && queueArray[j - 1] > value) {
queueArray[j] = queueArray[j - 1]; // Shifts every element up to make room for insertion
@ -99,7 +105,6 @@ class PriorityQueue{
* This class implements the PriorityQueue class above.
*
* @author Unknown
*
*/
public class PriorityQueues {
/**
@ -107,7 +112,7 @@ public class PriorityQueues{
*
* @param args Command Line Arguments
*/
public static void main(String args[]){
public static void main(String[] args) {
PriorityQueue myQueue = new PriorityQueue(4);
myQueue.insert(10);
myQueue.insert(2);