2019-05-09 19:32:54 +08:00
|
|
|
package DataStructures.Queues;
|
|
|
|
|
2018-04-14 11:45:48 +08:00
|
|
|
/**
|
|
|
|
* This class implements a PriorityQueue.
|
2020-10-24 18:23:28 +08:00
|
|
|
*
|
|
|
|
* <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.
|
2018-04-14 11:45:48 +08:00
|
|
|
*/
|
2019-02-17 09:18:50 +08:00
|
|
|
class PriorityQueue {
|
2020-10-24 18:23:28 +08:00
|
|
|
/** The max size of the queue */
|
|
|
|
private int maxSize;
|
|
|
|
/** The array for the queue */
|
|
|
|
private int[] queueArray;
|
|
|
|
/** How many items are in the queue */
|
|
|
|
private int nItems;
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param size Size of the queue
|
|
|
|
*/
|
|
|
|
public PriorityQueue(int size) {
|
|
|
|
maxSize = size;
|
|
|
|
queueArray = new int[size];
|
|
|
|
nItems = 0;
|
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Inserts an element in it's appropriate place
|
|
|
|
*
|
|
|
|
* @param value Value to be inserted
|
|
|
|
*/
|
|
|
|
public void insert(int value) {
|
|
|
|
if (isFull()) {
|
|
|
|
throw new RuntimeException("Queue is full");
|
|
|
|
} else {
|
|
|
|
int j = nItems - 1; // index of last element
|
|
|
|
while (j >= 0 && queueArray[j] > value) {
|
|
|
|
queueArray[j + 1] = queueArray[j]; // Shifts every element up to make room for insertion
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
queueArray[j + 1] = value; // Once the correct position is found the value is inserted
|
|
|
|
nItems++;
|
2019-02-17 09:18:50 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Remove the element from the front of the queue
|
|
|
|
*
|
|
|
|
* @return The element removed
|
|
|
|
*/
|
|
|
|
public int remove() {
|
|
|
|
return queueArray[--nItems];
|
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Checks what's at the front of the queue
|
|
|
|
*
|
|
|
|
* @return element at the front of the queue
|
|
|
|
*/
|
|
|
|
public int peek() {
|
|
|
|
return queueArray[nItems - 1];
|
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Returns true if the queue is empty
|
|
|
|
*
|
|
|
|
* @return true if the queue is empty
|
|
|
|
*/
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return (nItems == 0);
|
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Returns true if the queue is full
|
|
|
|
*
|
|
|
|
* @return true if the queue is full
|
|
|
|
*/
|
|
|
|
public boolean isFull() {
|
|
|
|
return (nItems == maxSize);
|
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Returns the number of elements in the queue
|
|
|
|
*
|
|
|
|
* @return number of elements in the queue
|
|
|
|
*/
|
|
|
|
public int getSize() {
|
|
|
|
return nItems;
|
|
|
|
}
|
2018-04-14 11:45:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class implements the PriorityQueue class above.
|
|
|
|
*
|
2019-02-17 09:18:50 +08:00
|
|
|
* @author Unknown
|
2018-04-14 11:45:48 +08:00
|
|
|
*/
|
2019-02-17 09:18:50 +08:00
|
|
|
public class PriorityQueues {
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Main method
|
|
|
|
*
|
|
|
|
* @param args Command Line Arguments
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
|
|
PriorityQueue myQueue = new PriorityQueue(4);
|
|
|
|
myQueue.insert(10);
|
|
|
|
myQueue.insert(2);
|
|
|
|
myQueue.insert(5);
|
|
|
|
myQueue.insert(3);
|
|
|
|
// [2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
for (int i = 3; i >= 0; i--)
|
|
|
|
System.out.print(
|
|
|
|
myQueue.remove() + " "); // will print the queue in reverse order [10, 5, 3, 2]
|
2018-04-14 11:45:48 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
// As you can see, a Priority Queue can be used as a sorting algotithm
|
|
|
|
}
|
2019-02-16 22:54:39 +08:00
|
|
|
}
|