mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 03:59:38 +08:00
Update PriorityQueues.java
This commit is contained in:
@ -1,21 +1,25 @@
|
|||||||
/**
|
/**
|
||||||
* This class implements a PriorityQueue.
|
* This class implements a PriorityQueue.
|
||||||
*
|
* <p>
|
||||||
* A priority queue adds elements into positions based on their priority.
|
* A priority queue adds elements into positions based on their priority.
|
||||||
* So the most important elements are placed at the front/on the top.
|
* 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.
|
* In this example I give numbers that are bigger, a higher priority.
|
||||||
* Queues in theory have no fixed size but when using an array
|
* Queues in theory have no fixed size but when using an array
|
||||||
* implementation it does.
|
* implementation it does.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
class PriorityQueue {
|
class PriorityQueue {
|
||||||
/** The max size of the queue */
|
/**
|
||||||
|
* The max size of the queue
|
||||||
|
*/
|
||||||
private int maxSize;
|
private int maxSize;
|
||||||
/** The array for the queue */
|
/**
|
||||||
|
* The array for the queue
|
||||||
|
*/
|
||||||
private int[] queueArray;
|
private int[] queueArray;
|
||||||
/** How many items are in the queue */
|
/**
|
||||||
|
* How many items are in the queue
|
||||||
|
*/
|
||||||
private int nItems;
|
private int nItems;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,22 +39,20 @@ class PriorityQueue{
|
|||||||
* @param value Value to be inserted
|
* @param value Value to be inserted
|
||||||
*/
|
*/
|
||||||
public void insert(int value) {
|
public void insert(int value) {
|
||||||
|
if (isFull()) {
|
||||||
|
throw new RuntimeException("Queue is full");
|
||||||
|
}
|
||||||
if (nItems == 0) {
|
if (nItems == 0) {
|
||||||
queueArray[0] = value;
|
queueArray[0] = value;
|
||||||
nItems++;
|
} else {
|
||||||
}
|
|
||||||
else if(isFull()){ //does not insert value when the queue is full
|
|
||||||
System.out.println("Queue is full");
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
int j = nItems;
|
int j = nItems;
|
||||||
while (j > 0 && queueArray[j - 1] > value) {
|
while (j > 0 && queueArray[j - 1] > value) {
|
||||||
queueArray[j] = queueArray[j - 1]; // Shifts every element up to make room for insertion
|
queueArray[j] = queueArray[j - 1]; // Shifts every element up to make room for insertion
|
||||||
j--;
|
j--;
|
||||||
}
|
}
|
||||||
queueArray[j] = value; // Once the correct position is found the value is inserted
|
queueArray[j] = value; // Once the correct position is found the value is inserted
|
||||||
nItems++;
|
|
||||||
}
|
}
|
||||||
|
nItems++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -103,7 +105,6 @@ class PriorityQueue{
|
|||||||
* This class implements the PriorityQueue class above.
|
* This class implements the PriorityQueue class above.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class PriorityQueues {
|
public class PriorityQueues {
|
||||||
/**
|
/**
|
||||||
@ -111,7 +112,7 @@ public class PriorityQueues{
|
|||||||
*
|
*
|
||||||
* @param args Command Line Arguments
|
* @param args Command Line Arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String args[]){
|
public static void main(String[] args) {
|
||||||
PriorityQueue myQueue = new PriorityQueue(4);
|
PriorityQueue myQueue = new PriorityQueue(4);
|
||||||
myQueue.insert(10);
|
myQueue.insert(10);
|
||||||
myQueue.insert(2);
|
myQueue.insert(2);
|
||||||
|
Reference in New Issue
Block a user