Does this code works like a priorityQueque? if not, wichc changes should i do to make it work like one, what should i do to improve it, using templates(any type), I have researched a lot about templates but couldnt find anything, I expected that this worked like a priorityQueque but I dont know if it does, other thing is that the remove method should work like when we use the remove metod in heapify(Compare the last child with the parent and if is smaller, change and continue until there is not more interactios, then change position and remove), should be helpful if somebody can help me with that
public class ColaDePrioridad {
private final int[] elements;
public int numberOfElements;
private final int capacity;
public ColaDePrioridad(int capacity) {
this.capacity= capacity;
elements = new int[capacity];
numberOfElements = 0;
}
public void add(int element) {
if (numberOfElements < capacity) {
elements[numberOfElements] = elements;
numberOfElements++;
} else {
throw new RuntimeException("The priorityQueque if full");
}
}
public void remove() {
if (numberOfElements == 0) {
throw new RuntimeException("There is no elements to remove");
} else {
int indexLeftChild, indexRightChild, MinorIndex;
for (int indexLastChild = elements.length - 1; indexLastChild >= 1;
indexLastChild --) {
for (int indexFather = (indexLastChild - 1) / 2; indexFather >= 0;
indexFather--) {
indexLeftChild= 2 * indexFather+ 1;
MinorIndex= indexFather;
if (indexLeftChild != indexLastChild ) {
indexRightChild = indexLeftChild+ 1;
if (elements[indexRightChild] < elements[indexFather]) {
MinorIndex= indexRightChild;
}
}
if (elements[indexLeftChild] < elements[indexFather]) {
MinorIndex = indexLeftChild;
}
change(elements, MinorIndex, indexFather);
}
change(elements, 0, elements.length - 1);
}
}
}
private void change(int[] elements, int indexElement1,
int indexElement2) {
int auxiliary = elements[indexElement1];
elements[indexElement1] = elements[indexElement2];
elements[indexElement2] = auxiliary;
}
public int getNumberOfElements() {
return numberOfElements;
}
method incompatible types: int[] cannot be converted to int elements[numberOfElements] = elements;
plusremove()
doesn't seem to remove anything, or at least doesn't decrementnumberOfElements
; and also proccesses array slots thatnumberOfElements
indicates haven't been logically set yet \$\endgroup\$