Inserting an element + priority on a linked list node

0

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);




 }
    
asked by anonymous 15.09.2018 / 23:56

0 answers