2
I need to add an element at the end of a linked list, I did the method asking it to add as if it were an initial element if the list is empty (it is working) and, if it is not, scrolling through the list as follows:
else{
Celula * cursor = prim;
while (cursor != NULL){
cursor = cursor -> getProx();
For me, when it arrives at the stop condition, it is because the cursor = NULL, creating another condition to add the element that would be the following:
if (cursor == NULL){
Celula * c = new Celula(i);
c->setProx(prim);
cout << cursor->getInfo()<< endl;
When I run the program, I can insert the first element normally, but when I insert it at the end, the program breaks.
If someone needs the full program to understand better (sorry if I’m not good with rs words), follow the link of the file in repl.it whose problem is in Liston.cpp
https://repl.it/@RodrigoCosta1/beijonabocaecoisadopassadoagramodaefazerlistaligada
(ignore the link title)
If it stops in
NULL
in making->
anything aboutNULL
crash. Change the condition of thewhile
forwhile (cursor->getProx() != NULL){
– Isac
I switched and he didn’t crash, but he’s still not adding anything from the end.
– tsnotrod
This already has to do with the defining the next part. Looking at your code and without testing it would be something like following the
while
, doCelula * c = new Celula(i);c->setProx(NULL);cursor->setProx(c);
– Isac