Code to get the biggest number fucked up?

Asked

Viewed 93 times

0

I created the code that is in the image to go through a double chained list and get the biggest token(No number), and sent the following entry(add at the end) : 12, 17, 14, 23, 8, 10. It should be removed 23 and when I call again the method would remove 17, but it is removing 10 and 23. What’s wrong? Eh bug? or my mistake?

inserir a descrição da imagem aqui

public class ListaNOrdenada {
    private No inicio;
    private int tam;
    private No fim;

    public void add(Integer num) {
        No n = new No();
        n.setToken(num);
        if(tam == 0) {
            inicio = n;
            fim = n;
        }else{
            fim.setProx(n);
            n.setAnt(fim);
            fim = n;
        }
        tam++;
    }

    public No remover() {
        No aux = inicio;
        No maior = inicio;
        int i = 0;
        while(aux.getProx() != null) {
            if(aux.getToken() < aux.getProx().getToken()) {
                maior = aux.getProx();
                System.out.println("maior: " +maior.getToken());
            }
            aux = aux.getProx();
            i++;
        }
        System.out.println("maior: " +maior.getToken());
        if(maior == inicio) {
            inicio = inicio.getProx();
            inicio.setAnt(null);
        }else if(maior == fim) {
            fim = fim.getAnt();
            fim.setProx(null);
        }else{
            maior.getAnt().setProx(maior.getProx());
            maior.getProx().setAnt(maior.getAnt());
        }
        tam--;
        return maior;

    }

    public void imprimir() {
        No aux = inicio;
        System.out.println(aux.getToken());
        while(aux.getProx() != null) {
            aux = aux.getProx();
            System.out.println(aux.getToken());
        }
    }
}
`
  • 1

    Put the code in the post instead of raising an image of it that we help.

  • There is the class with the methods, will need the other?

No answers

Browser other questions tagged

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