Skip to content

Commit b73757e

Browse files
authored
Update PriorityQueues.java
Include condition to check if the queue is full when inserting values into the queue
1 parent 46bbfaa commit b73757e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

DataStructures/Queues/PriorityQueues.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public PriorityQueue(int size){
3737
public void insert(int value){
3838
if(nItems == 0){
3939
queueArray[0] = value;
40+
nItems++;
41+
}
42+
else if(isFull()){ //does not insert value when the queue is full
43+
System.out.println("Queue is full");
4044
}
4145
else{
4246
int j = nItems;
@@ -45,8 +49,8 @@ public void insert(int value){
4549
j--;
4650
}
4751
queueArray[j] = value; //Once the correct position is found the value is inserted
52+
nItems++;
4853
}
49-
nItems++;
5054
}
5155

5256
/**
@@ -120,4 +124,4 @@ public static void main(String args[]){
120124

121125
//As you can see, a Priority Queue can be used as a sorting algotithm
122126
}
123-
}
127+
}

0 commit comments

Comments
 (0)