-2
\$\begingroup\$

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;
}
New contributor
Mauricio Rios Cantillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to Code review! Sadly, Code not working as intended is off-topic here. I'm getting a compiler error in the add method incompatible types: int[] cannot be converted to int elements[numberOfElements] = elements; plus remove() doesn't seem to remove anything, or at least doesn't decrement numberOfElements; and also proccesses array slots that numberOfElements indicates haven't been logically set yet \$\endgroup\$
    – Jannik S.
    Commented Apr 21 at 5:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.