/**
* Priority queue
* */
public class PriorityQ {
private int mSize ;
private int[] datas;
private int itemIndex;
public PriorityQ (int s) {
mSize = s;
itemIndex = -1;
datas = new int[mSize];
}
public void insert (int item) {
if (itemIndex == mSize - 1) throw new IllegalStateException("the priority is full.");
if (itemIndex == -1) {
datas[++itemIndex] = item;
} else {
for (int i = itemIndex; i >= 0; i--) {
System.out.println(i + " = i : " + datas[i]);
if (item > datas[i]) {
datas[i + 1] = datas[i];
} else {
datas[i + 1] = item;
itemIndex ++;
break;
}
datas[i] = item;
itemIndex ++;
}
}
}
/**
* check front
* @return
*/
public int peek () {
if (itemIndex == -1) throw new IllegalStateException("the priority is empty.");
return datas[itemIndex];
}
/**
* check tail
* @return
*/
public int peekTail () {
if (itemIndex == -1) throw new IllegalStateException("the priority is empty.");
return datas[0];
}
/**
* remove tail
* @return
*/
public int removeTail () {
if (itemIndex == -1) throw new IllegalStateException("the priority is empty.");
int remove = datas[0];
for (int i = 0; i < itemIndex ; i++) {
datas[i] = datas[i+1];
}
itemIndex --;
return remove;
}
/**
* remove front
* @return
*/
public int remove () {
if (itemIndex == -1) throw new IllegalStateException("the priority is empty.");
int remove = datas[itemIndex];
itemIndex --;
return remove;
}
public int size () {
return itemIndex + 1;
}
}