Skip to content

Commit 36d6b51

Browse files
authored
Update PriorityQueues.java
1 parent b73757e commit 36d6b51

File tree

1 file changed

+103
-102
lines changed

1 file changed

+103
-102
lines changed
+103-102
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,128 @@
11
/**
22
* This class implements a PriorityQueue.
3-
*
3+
* <p>
44
* A priority queue adds elements into positions based on their priority.
55
* So the most important elements are placed at the front/on the top.
66
* In this example I give numbers that are bigger, a higher priority.
77
* Queues in theory have no fixed size but when using an array
88
* implementation it does.
9-
*
10-
* @author Unknown
119
*
1210
*/
13-
class PriorityQueue{
14-
/** The max size of the queue */
15-
private int maxSize;
16-
/** The array for the queue */
17-
private int[] queueArray;
18-
/** How many items are in the queue */
19-
private int nItems;
11+
class PriorityQueue {
12+
/**
13+
* The max size of the queue
14+
*/
15+
private int maxSize;
16+
/**
17+
* The array for the queue
18+
*/
19+
private int[] queueArray;
20+
/**
21+
* How many items are in the queue
22+
*/
23+
private int nItems;
2024

21-
/**
22-
* Constructor
23-
*
24-
* @param size Size of the queue
25-
*/
26-
public PriorityQueue(int size){
27-
maxSize = size;
28-
queueArray = new int[size];
29-
nItems = 0;
30-
}
25+
/**
26+
* Constructor
27+
*
28+
* @param size Size of the queue
29+
*/
30+
public PriorityQueue(int size) {
31+
maxSize = size;
32+
queueArray = new int[size];
33+
nItems = 0;
34+
}
3135

32-
/**
33-
* Inserts an element in it's appropriate place
34-
*
35-
* @param value Value to be inserted
36-
*/
37-
public void insert(int value){
38-
if(nItems == 0){
39-
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");
44-
}
45-
else{
46-
int j = nItems;
47-
while(j > 0 && queueArray[j-1] > value){
48-
queueArray[j] = queueArray[j-1]; //Shifts every element up to make room for insertion
49-
j--;
50-
}
51-
queueArray[j] = value; //Once the correct position is found the value is inserted
52-
nItems++;
53-
}
54-
}
36+
/**
37+
* Inserts an element in it's appropriate place
38+
*
39+
* @param value Value to be inserted
40+
*/
41+
public void insert(int value) {
42+
if (isFull()) {
43+
throw new RuntimeException("Queue is full");
44+
}
45+
if (nItems == 0) {
46+
queueArray[0] = value;
47+
} else {
48+
int j = nItems;
49+
while (j > 0 && queueArray[j - 1] > value) {
50+
queueArray[j] = queueArray[j - 1]; // Shifts every element up to make room for insertion
51+
j--;
52+
}
53+
queueArray[j] = value; // Once the correct position is found the value is inserted
54+
}
55+
nItems++;
56+
}
5557

56-
/**
57-
* Remove the element from the front of the queue
58-
*
59-
* @return The element removed
60-
*/
61-
public int remove(){
62-
return queueArray[--nItems];
63-
}
58+
/**
59+
* Remove the element from the front of the queue
60+
*
61+
* @return The element removed
62+
*/
63+
public int remove() {
64+
return queueArray[--nItems];
65+
}
6466

65-
/**
66-
* Checks what's at the front of the queue
67-
*
68-
* @return element at the front of the queue
69-
*/
70-
public int peek(){
71-
return queueArray[nItems-1];
72-
}
67+
/**
68+
* Checks what's at the front of the queue
69+
*
70+
* @return element at the front of the queue
71+
*/
72+
public int peek() {
73+
return queueArray[nItems - 1];
74+
}
7375

74-
/**
75-
* Returns true if the queue is empty
76-
*
77-
* @return true if the queue is empty
78-
*/
79-
public boolean isEmpty(){
80-
return(nItems == 0);
81-
}
76+
/**
77+
* Returns true if the queue is empty
78+
*
79+
* @return true if the queue is empty
80+
*/
81+
public boolean isEmpty() {
82+
return (nItems == 0);
83+
}
8284

83-
/**
84-
* Returns true if the queue is full
85-
*
86-
* @return true if the queue is full
87-
*/
88-
public boolean isFull(){
89-
return(nItems == maxSize);
90-
}
85+
/**
86+
* Returns true if the queue is full
87+
*
88+
* @return true if the queue is full
89+
*/
90+
public boolean isFull() {
91+
return (nItems == maxSize);
92+
}
9193

92-
/**
93-
* Returns the number of elements in the queue
94-
*
95-
* @return number of elements in the queue
96-
*/
97-
public int getSize(){
98-
return nItems;
99-
}
94+
/**
95+
* Returns the number of elements in the queue
96+
*
97+
* @return number of elements in the queue
98+
*/
99+
public int getSize() {
100+
return nItems;
101+
}
100102
}
101103

102104
/**
103105
* This class implements the PriorityQueue class above.
104-
*
105-
* @author Unknown
106106
*
107+
* @author Unknown
107108
*/
108-
public class PriorityQueues{
109-
/**
110-
* Main method
111-
*
112-
* @param args Command Line Arguments
113-
*/
114-
public static void main(String args[]){
115-
PriorityQueue myQueue = new PriorityQueue(4);
116-
myQueue.insert(10);
117-
myQueue.insert(2);
118-
myQueue.insert(5);
119-
myQueue.insert(3);
120-
//[2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
109+
public class PriorityQueues {
110+
/**
111+
* Main method
112+
*
113+
* @param args Command Line Arguments
114+
*/
115+
public static void main(String[] args) {
116+
PriorityQueue myQueue = new PriorityQueue(4);
117+
myQueue.insert(10);
118+
myQueue.insert(2);
119+
myQueue.insert(5);
120+
myQueue.insert(3);
121+
// [2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
121122

122-
for(int i = 3; i>=0; i--)
123-
System.out.print(myQueue.remove() + " "); //will print the queue in reverse order [10, 5, 3, 2]
123+
for (int i = 3; i >= 0; i--)
124+
System.out.print(myQueue.remove() + " "); // will print the queue in reverse order [10, 5, 3, 2]
124125

125-
//As you can see, a Priority Queue can be used as a sorting algotithm
126-
}
126+
// As you can see, a Priority Queue can be used as a sorting algotithm
127+
}
127128
}

0 commit comments

Comments
 (0)