3
I’m implementing a chained list in Java. However, the removing function is causing me some problems, but specifically the removing part in the middle of the list. My list consists only of a field of the whole type that has a note.
public void remove(int nota)
{
Aluno ante = null;
Aluno posi = this.first;
while(posi.getNota() != nota)
{
ante = posi;
posi = posi.getProximo();
}
if(posi == null)
{
System.out.println("Valor não encontrado");
return;
}
if(posi == this.first)
{
this.first = posi.getProximo();
posi = null;
}
else if(posi.getProximo() == null)
{
ante.setProximo(null);
posi = null;
}
else
{
ante.setProximo(posi);
posi = null;
}
this.tamLista--;
}
Where’s the bug? I can’t identify.
I couldn’t implement recursive code, because in the following line: student.getProx() = removeAluno(student.getProx(), note); I get an error: the left hand side of an assignment must be a variable
– Vinicius
@Vinicius, The Student class must contain an arybute
int nota
and an attributeAluno proximoAluno
. Therefore the methodgetProx()
must return the value ofproximoAluno
of an objectAluno
. You are implementing this way?– regmoraes
@Vinicius, instead of doing direct assignment, ie, x = y, create methods set in your class Pupil, this way, when you want to replace one knot by the other, do
aluno.set(outroNoAluno)
.– regmoraes