public class PriorityQueue {
private int maxSize;
private Passenger[] queueArray;
private int nItems, front, lowerBound, upperBound, curIn;
public PriorityQueue(int s) {
maxSize = s;
queueArray = new Passenger[maxSize];
front = 0;
nItems = 0;
}
public void insert(Passenger something) {
int j;
if(nItems ==0) {
queueArray[nItems++] = something;}
else {
for (j = nItems -1; j>=0; j--) {
if (something.getNumber() < queueArray[j].getNumber())
{
queueArray[j +1] = queueArray[j];
} else {
break;
}
}
queueArray[j +1] = something;
nItems++;
}
}
public Passenger find(int F) {
lowerBound = 0;
upperBound = nItems - 1;
while (lowerBound <= upperBound) {
curIn = (lowerBound + upperBound) /2;
if (queueArray[curIn].getNumber() ==F) {
System.out.println(F + " found at location " + curIn);
break;
} else if (queueArray[curIn].getNumber() < F) {
lowerBound = curIn+1;
} else {
upperBound = curIn-1;
}
return queueArray[curIn];
}
return null;
}
public Passenger remove() {
Passenger temp = queueArray[front++];
nItems--;
return temp;
}
public String peek(int j) {
return queueArray[j].displayPassenger();
}
public Passenger peekFront() {
return queueArray[front];
}
public boolean isEmpty() {
return (nItems ==0); }
public boolean isFull() {
return (nItems == maxSize);
}
}
public class PriorityQueueApp {
public static void main(String[] args) {
int maxSize = 10;
PriorityQueue arr;
arr = new PriorityQueue(maxSize);
Passenger[] p = new Passenger[maxSize];
p[0] = new Passenger(105, "Hashimoto", "Sato", 21);
p[1] = new Passenger(103, "Yee", "Tom", 43);
p[2] = new Passenger(101, "Evans", "Patty", 24);
p[3] = new Passenger(102, "Smith", "Lorraine", 37);
p[4] = new Passenger(104, "Adams", "Henry", 63);
p[5] = new Passenger(106, "Stimson", "Henry", 29);
p[6] = new Passenger(109, "Vang", "Minh", 22);
p[7] = new Passenger(107, "Velasquez", "Jose", 72);
p[8] = new Passenger(108, "Lamarque", "Henry", 54);
p[9] = new Passenger(110, "Creswell", "Lucinda", 18);
for (int i =0; i< maxSize; i++)
System.out.println(p[i].displayPassenger());
System.out.println("");
for (int j=0; j < maxSize; j++)
arr.insert(p[j]);
int searchNumber = 106;
Passenger found;
found = arr.find(searchNumber);
if (found != null) {
System.out.print("Found ");
System.out.println(found.displayPassenger());
System.out.println("");
}
while (!arr.isEmpty()) {
Passenger n = arr.remove();
System.out.print(n.displayPassenger());
System.out.println("");
}
for (int h = 0; h < maxSize; h++) {
System.out.println(arr.peek(h));
arr.remove();
}
}
}