Update PriorityQueues.java
This commit is contained in:
parent
b73757e4bb
commit
36d6b5133f
@ -1,21 +1,25 @@
|
||||
/**
|
||||
* This class implements a PriorityQueue.
|
||||
*
|
||||
* <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.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class PriorityQueue{
|
||||
/** The max size of the queue */
|
||||
class PriorityQueue {
|
||||
/**
|
||||
* The max size of the queue
|
||||
*/
|
||||
private int maxSize;
|
||||
/** The array for the queue */
|
||||
/**
|
||||
* The array for the queue
|
||||
*/
|
||||
private int[] queueArray;
|
||||
/** How many items are in the queue */
|
||||
/**
|
||||
* How many items are in the queue
|
||||
*/
|
||||
private int nItems;
|
||||
|
||||
/**
|
||||
@ -23,7 +27,7 @@ class PriorityQueue{
|
||||
*
|
||||
* @param size Size of the queue
|
||||
*/
|
||||
public PriorityQueue(int size){
|
||||
public PriorityQueue(int size) {
|
||||
maxSize = size;
|
||||
queueArray = new int[size];
|
||||
nItems = 0;
|
||||
@ -34,23 +38,21 @@ class PriorityQueue{
|
||||
*
|
||||
* @param value Value to be inserted
|
||||
*/
|
||||
public void insert(int value){
|
||||
if(nItems == 0){
|
||||
public void insert(int value) {
|
||||
if (isFull()) {
|
||||
throw new RuntimeException("Queue is full");
|
||||
}
|
||||
if (nItems == 0) {
|
||||
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;
|
||||
while(j > 0 && queueArray[j-1] > value){
|
||||
queueArray[j] = queueArray[j-1]; //Shifts every element up to make room for insertion
|
||||
while (j > 0 && queueArray[j - 1] > value) {
|
||||
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
|
||||
nItems++;
|
||||
queueArray[j] = value; // Once the correct position is found the value is inserted
|
||||
}
|
||||
nItems++;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,7 +60,7 @@ class PriorityQueue{
|
||||
*
|
||||
* @return The element removed
|
||||
*/
|
||||
public int remove(){
|
||||
public int remove() {
|
||||
return queueArray[--nItems];
|
||||
}
|
||||
|
||||
@ -67,8 +69,8 @@ class PriorityQueue{
|
||||
*
|
||||
* @return element at the front of the queue
|
||||
*/
|
||||
public int peek(){
|
||||
return queueArray[nItems-1];
|
||||
public int peek() {
|
||||
return queueArray[nItems - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -76,8 +78,8 @@ class PriorityQueue{
|
||||
*
|
||||
* @return true if the queue is empty
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
return(nItems == 0);
|
||||
public boolean isEmpty() {
|
||||
return (nItems == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,8 +87,8 @@ class PriorityQueue{
|
||||
*
|
||||
* @return true if the queue is full
|
||||
*/
|
||||
public boolean isFull(){
|
||||
return(nItems == maxSize);
|
||||
public boolean isFull() {
|
||||
return (nItems == maxSize);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,7 +96,7 @@ class PriorityQueue{
|
||||
*
|
||||
* @return number of elements in the queue
|
||||
*/
|
||||
public int getSize(){
|
||||
public int getSize() {
|
||||
return nItems;
|
||||
}
|
||||
}
|
||||
@ -103,25 +105,24 @@ class PriorityQueue{
|
||||
* This class implements the PriorityQueue class above.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class PriorityQueues{
|
||||
public class PriorityQueues {
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command Line Arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
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
|
||||
// [2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
|
||||
|
||||
for(int i = 3; i>=0; i--)
|
||||
System.out.print(myQueue.remove() + " "); //will print the queue in reverse order [10, 5, 3, 2]
|
||||
for (int i = 3; i >= 0; i--)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user