Update PriorityQueues.java

This commit is contained in:
Libin Yang 2019-02-17 09:18:50 +08:00 committed by GitHub
parent b73757e4bb
commit 36d6b5133f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,127 +1,128 @@
/** /**
* 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 */ /**
private int maxSize; * The max size of the queue
/** The array for the queue */ */
private int[] queueArray; private int maxSize;
/** How many items are in the queue */ /**
private int nItems; * The array for the queue
*/
private int[] queueArray;
/**
* How many items are in the queue
*/
private int nItems;
/** /**
* Constructor * Constructor
* *
* @param size Size of the queue * @param size Size of the queue
*/ */
public PriorityQueue(int size){ public PriorityQueue(int size) {
maxSize = size; maxSize = size;
queueArray = new int[size]; queueArray = new int[size];
nItems = 0; nItems = 0;
} }
/** /**
* Inserts an element in it's appropriate place * Inserts an element in it's appropriate place
* *
* @param value Value to be inserted * @param value Value to be inserted
*/ */
public void insert(int value){ public void insert(int value) {
if(nItems == 0){ if (isFull()) {
queueArray[0] = value; throw new RuntimeException("Queue is full");
nItems++; }
} if (nItems == 0) {
else if(isFull()){ //does not insert value when the queue is full queueArray[0] = value;
System.out.println("Queue is full"); } else {
} int j = nItems;
else{ while (j > 0 && queueArray[j - 1] > value) {
int j = nItems; queueArray[j] = queueArray[j - 1]; // Shifts every element up to make room for insertion
while(j > 0 && queueArray[j-1] > value){ j--;
queueArray[j] = queueArray[j-1]; //Shifts every element up to make room for insertion }
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
* *
* @return The element removed * @return The element removed
*/ */
public int remove(){ public int remove() {
return queueArray[--nItems]; return queueArray[--nItems];
} }
/** /**
* Checks what's at the front of the queue * Checks what's at the front of the queue
* *
* @return element at the front of the queue * @return element at the front of the queue
*/ */
public int peek(){ public int peek() {
return queueArray[nItems-1]; return queueArray[nItems - 1];
} }
/** /**
* Returns true if the queue is empty * Returns true if the queue is empty
* *
* @return true if the queue is empty * @return true if the queue is empty
*/ */
public boolean isEmpty(){ public boolean isEmpty() {
return(nItems == 0); return (nItems == 0);
} }
/** /**
* Returns true if the queue is full * Returns true if the queue is full
* *
* @return true if the queue is full * @return true if the queue is full
*/ */
public boolean isFull(){ public boolean isFull() {
return(nItems == maxSize); return (nItems == maxSize);
} }
/** /**
* Returns the number of elements in the queue * Returns the number of elements in the queue
* *
* @return number of elements in the queue * @return number of elements in the queue
*/ */
public int getSize(){ public int getSize() {
return nItems; return nItems;
} }
} }
/** /**
* This class implements the PriorityQueue class above. * This class implements the PriorityQueue class above.
*
* @author Unknown
* *
* @author Unknown
*/ */
public class PriorityQueues{ public class PriorityQueues {
/** /**
* Main method * Main method
* *
* @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);
myQueue.insert(5); myQueue.insert(5);
myQueue.insert(3); myQueue.insert(3);
//[2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top // [2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
for(int i = 3; i>=0; i--) for (int i = 3; i >= 0; i--)
System.out.print(myQueue.remove() + " "); //will print the queue in reverse order [10, 5, 3, 2] System.out.print(myQueue.remove() + " "); // will print the queue in reverse order [10, 5, 3, 2]
//As you can see, a Priority Queue can be used as a sorting algotithm // As you can see, a Priority Queue can be used as a sorting algotithm
} }
} }