Create a queue that Items are inserted in the proper position to maintain the order in Java
Description
The following code shows how to create a queue that Items are inserted in the proper position to maintain the order.
Example
class PriorityQueue {
/*from w w w . ja v a2 s .c o m*/
private int maxSize;
private double[] queArray;
private int nItems;
public PriorityQueue(int s) {
maxSize = s;
queArray = new double[maxSize];
nItems = 0;
}
public void insert(double item) {
int j;
if (nItems == 0) {
queArray[nItems++] = item;
} else {
for (j = nItems - 1; j >= 0; j--)
{
if (item > queArray[j])
queArray[j + 1] = queArray[j];
else
break;
}
queArray[j + 1] = item;
nItems++;
}
}
public double remove() {
return queArray[--nItems];
}
public double peekMin() {
return queArray[nItems - 1];
}
public boolean isEmpty() {
return (nItems == 0);
}
public boolean isFull() {
return (nItems == maxSize);
}
}
public class Main {
public static void main(String[] args) {
PriorityQueue thePQ = new PriorityQueue(5);
thePQ.insert(30);
thePQ.insert(50);
thePQ.insert(10);
thePQ.insert(40);
thePQ.insert(20);
while (!thePQ.isEmpty()) {
double item = thePQ.remove();
System.out.print(item + " ");
}
}
}
The code above generates the following result.