Problem removing element from a chained list

Asked

Viewed 461 times

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.

2 answers

4


A simpler way is to use a recursive method. This way, you will pass as parameter the first element of the list and the note you want to search for to remove. An example:

Aluno removeAluno(Aluno aluno,int nota){

  if(aluno != null){

      // Se o aluno atual for o aluno que você deseja remover, ele é substituido pelo aluno seguinte
     if(aluno.getNota() == nota){

      aluno = aluno.getProx();

     // Se não for, a função é chamada novamente, passando o próximo aluno como parâmetro
    }else{

        aluno.getProx() = removeAluno(aluno.getProx(), nota);
    }
  }

  return aluno;
}

Example of execution:

List: Student(note:10)-> Student(note:8)-> Student(note:6) -> Student(note:5)

Find note == 6

Execution:

  1. Aluno(nota:10) = removeAluno( Aluno(nota:10), 6)
  2. 6 == Aluno(nota:10).getNota() ----> false
  3. Aluno(nota:10).getProx() = removeAluno( Aluno(nota:8) , 6)
  4. 6 == Aluno(nota:8).getNota() ----> false
  5. Aluno(nota:8).getProx() = removeAluno( Aluno(nota:6) , 6)
  6. 6 == Aluno(nota:6).getNota() ----> true
  7. Aluno(nota:6) é substituído por Aluno(nota:5)

Now begins the recursion, the Aluno(nota:5) who has just replaced Aluno(nota:6), is returned to previous calls. Doing it backwards:

  1. Aluno(nota:8).getProx() = Aluno(nota:5) that is to say: Aluno(nota:8)-> Aluno(nota:5)
  2. Aluno(nota:10).getProx() = Aluno(nota:8) that is to say: Aluno(nota:10)-> Aluno(nota:8)
  3. Aluno(nota:10) = Aluno(nota:10) i.e., the first element of the list remains Student(note:10)

If we look at the list now, she’ll be like this:

List: Student(note:10)-> Student(note:8)-> Student(note:5)

  • 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, The Student class must contain an arybute int nota and an attribute Aluno proximoAluno. Therefore the method getProx() must return the value of proximoAluno of an object Aluno. You are implementing this way?

  • @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).

0

Friend, it is not possible to do some operations in lists at runtime. For example, if the list is inside "for" chained it is not possible to remove. You need to change the logic to get to your goal. Have how to post the log with the error to check better?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.