@@ -0,0 +1,1903 @@
+/*%0A * A priority queue adds elements into positions based on their priority.%0A * So the most important elements are placed at the front/on the top.%0A * In this example I give numbers that are bigger, a higher priority.%0A * Queues in theory have no fixed size but when using an array implementation it does.%0A */%0Aclass PriorityQueue%7B%0A%09private int maxSize;%0A%09private int%5B%5D queueArray;%0A%09private int nItems;%0A%0A%09public PriorityQueue(int size)%7B //Constructor%0A%09%09maxSize = size;%0A%09%09queueArray = new int%5Bsize%5D;%0A%09%09nItems = 0;%0A%09%7D%0A%0A%09public void insert(int value)%7B //Inserts an element in it's appropriate place%0A%09%09if(nItems == 0)%7B%0A%09%09%09queueArray%5B0%5D = value;%0A%09%09%7D%0A%09%09else%7B%0A%09%09%09int j = nItems;%0A%09%09%09while(j %3E 0 && queueArray%5Bj-1%5D %3E value)%7B%0A%09%09%09%09queueArray%5Bj%5D = queueArray%5Bj-1%5D; //Shifts every element up to make room for insertion%0A%09%09%09%09j--;%0A%09%09%09%7D%0A%09%09%09queueArray%5Bj%5D = value; //Once the correct position is found the value is inserted%0A%09%09%7D%0A%09%09nItems++;%0A%09%7D%0A%0A%09public int remove()%7B //Remove the element from the front of the queue%0A%09%09return queueArray%5B--nItems%5D;%0A%09%7D%0A%0A%09public int peek()%7B //Checks what's at the front of the queue%0A%09%09return queueArray%5BnItems-1%5D;%0A%09%7D%0A%0A%09public boolean isEmpty()%7B //Returns true is the queue is empty%0A%09%09return(nItems == 0);%0A%09%7D%0A%0A%09public boolean isFull()%7B //Returns true is the queue is full%0A%09%09return(nItems == maxSize);%0A%09%7D%0A%0A%09public int getSize()%7B //Returns the number of elements in the queue%0A%09%09return nItems;%0A%09%7D%0A%7D%0A//Example%0Apublic class PriorityQueues%7B%0A%09public static void main(String args%5B%5D)%7B%0A%09%09PriorityQueue myQueue = new PriorityQueue(4);%0A%09%09myQueue.insert(10);%0A%09%09myQueue.insert(2);%0A%09%09myQueue.insert(5);%0A%09%09myQueue.insert(3);%0A%09%09//%5B2, 3, 5, 10%5D Here higher numbers have higher priority, so they are on the top%0A%0A%09%09for(int i = 3; i%3E=0; i--)%0A%09%09%09System.out.print(myQueue.remove() + %22 %22); //will print the queue in reverse order %5B10, 5, 3, 2%5D%0A%0A%09%09//As you can see, a Priority Queue can be used as a sorting algotithm%0A%09%7D%0A%7D%0A

Create PriorityQueues.java