mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-14 01:16:07 +08:00
Update PriorityQueues.java
Include condition to check if the queue is full when inserting values into the queue
This commit is contained in:
@ -37,6 +37,10 @@ class PriorityQueue{
|
|||||||
public void insert(int value){
|
public void insert(int value){
|
||||||
if(nItems == 0){
|
if(nItems == 0){
|
||||||
queueArray[0] = value;
|
queueArray[0] = value;
|
||||||
|
nItems++;
|
||||||
|
}
|
||||||
|
else if(isFull()){ //does not insert value when the queue is full
|
||||||
|
System.out.println("Queue is full");
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
int j = nItems;
|
int j = nItems;
|
||||||
@ -45,9 +49,9 @@ class PriorityQueue{
|
|||||||
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++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the element from the front of the queue
|
* Remove the element from the front of the queue
|
||||||
|
Reference in New Issue
Block a user