Chained list is not removing the elements

Asked

Viewed 137 times

0

I cannot remove elements from the list.

class no {

    public int dado;
    public no prox;
}

class lista {

    no ini;
    no fim;

    public void crialista() {

        ini = null;
        fim = null;
    }

    public void inserir(int num) {
        no novo = new no();
        novo.dado = num;
        novo.prox = null;

        if (ini == null) {
            ini = novo;
        } else {
            fim.prox = novo;
        }
        fim = novo;
    }

    public void imp() {
        no x = ini;
        while (x != null) {
            System.out.println(x.dado);
            x = x.prox;
        }
    }

    public void remover(int num){


    no ant = null;
    no aux = ini;

    do {
    if (aux.dado== num )    
    {
    ant.prox= ant.prox;
    }

    ant = aux;
    aux = aux.prox;
} while (aux!=null);



}

public  class Teste {


    public static void main(String[] args) {

        lista lst = new lista();
        lst.crialista();
        lst.inserir(15);
        lst.inserir(8);
        lst.imp();
        lst.remover (8);
        lst.imp();
    }
}
}
  • I don’t know if it’s the cause but this: ant.prox= ant.prox; it does not seem to me that it is/is correct.

  • the error program in the method.

1 answer

1

First let’s organize the code, mainly giving more meaningful names to make what we’re doing more intuitive (at least eliminate a generic name aux for atual). Then it’s easy to see the error:

public void remover(int num){
    no ant = null;
    no atual = ini;
    do {
        if (atual.dado == num) ant.prox = atual.prox; //você tem que pegar o próximo do atual
        ant = atual;
        atual = atual.prox;
    } while (atual != null);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

It wasn’t changing anything, it’s almost a typo. It happens because the code isn’t very readable. Many people only understand the power of readability when they begin to have these problems. Other things could be even more readable. It would be better if ant turn anterior to promote readability of.

This code still has problems. If the list has no elements gives problem, the ideal would be to at least check this situation before making any operation. Probably just reversing the do-while for while already solves. Of course there will be information failure, but that the whole code is not worrying.

If it is to do the ideal could have a search method and the removal method would call this search and would only do the removal. The verification of whether there are elements could be done by a method of its own, and so on. Each with their own responsibility without exposing unnecessary details.

I could initialize the list even without a method for it.

  • Thank you so much, I’ve been racking my brain since Saturday with this, I’m gonna need to sort the numbers when they’re printed, can you give me a hint?

  • Here is another question. There are some techniques, start by researching about chained list ordering. It is a little more complicated, but it follows the line of removal. One of the basic techniques is to reverse the prox current with the previous as the smaller form. (or larger depending on how you are doing).

  • Thank you very much, you helped too much.

  • Better than thanks, is to practice the use of the site. See the [tour].

Browser other questions tagged

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