I am creating a method to insert into a linked list an ordered element according to the priority but it does not insert correctly, the first element it inserts normally but the rest it inserts at the end of the list and duplicates the same
bool ListaLigada::inserePrio(int elem, int prio){
if (eVazia()){
insereInicio(elem, prio);
return -1;
}
Celula *cursor = prim;
Celula *c = new Celula(elem,prio);
//if(prio > prim-> getPrio()){
// cursor ->setProx(c);
// c ->setProx(NULL);
//c->setProx(prim);
//prim = c;
//}
while (cursor != NULL){
if (cursor -> getProx() == NULL && cursor->getPrio() < prio){
insereFinal(elem, prio);
break;
}
if (cursor->getProx()->getPrio() < prio)
break;
cursor = cursor->getProx();
if (cursor -> getPrio() < prio){
cursor = cursor-> getProx();
}
if (cursor -> getPrio() > prio){
Celula * c = new Celula(elem,prio);
c -> setProx (cursor->getProx());
cursor -> setProx(c);
break;
}
}
c->setProx(cursor->getProx());
cursor->setProx(c);
}