Data Structure, Double-chained List. Doubt about the constructor of the DoubleList class

1
  

Note: Code taken from a Data Structure workbook.

     

Why not pass the number of nodes in the builder's Duplicated Directory (such as in Stack or Queue structures)?

public class ListaDupla {
    private NoDupla prim;
    private NoDupla ult;
    private int quantNos;

    public ListaDupla(){
        this.prim = null;
        this.ult = null;
        this.quantNos = 0;
    }
    public int getQuantNos(){
        return this.quantNos;
    }
    public NoDupla getPrim(){
        return this.prim;
    }
    public NoDupla getUlt(){
        return this.ult;
    }
    public void setQuantNos(int valorNovo){
        this.quantNos = valorNovo;
    }
    public void setPrim(NoDupla novoNo){
        this.prim = novoNo;
    }
    public void setUlt(NoDupla novoNo){
        this.ult = novoNo;
    }
    public boolean eVazia (){
        return (this.prim == null);
    }
    public void inserirPrimeiro(Item elem){
        NoDupla novoNo = new NoDupla (elem);
        if (this.eVazia())
            this.ult = novoNo;
        else { 
            novoNo.setProx(this.prim);
            this.prim.setAnt(novoNo);
        }
        this.prim = novoNo;
        this.quantNos++;
    }
    public void inserirUltimo (Item elem){
        NoDupla novoNo = new NoDupla (elem);
        if (this.eVazia())
            this.prim = novoNo;
        else { 
            novoNo.setAnt(this.ult);
            this.ult.setProx(novoNo);
        }   
        this.ult = novoNo;
        this.quantNos++;
    }
    public NoDupla pesquisarNo (int chave){
        NoDupla atual = this.prim;
        while ((atual != null) && (atual.getInfo().getChave() != chave))
            atual = atual.getProx();
        return atual;
    }
    public boolean removerNo (int chave){
        NoDupla atual = this.prim;
        while ((atual != null) && (atual.getInfo().getChave()!= chave)){
            atual = atual.getProx();
        }
        if (atual == null)
            return false;
        else 
            if (atual == this.prim){
                this.prim = prim.getProx();
                if (this.prim == null)
                    this.ult=null;
                else 
                    this.prim.setAnt(null);
            }
            else 
                if (atual == this.ult){
                    this.ult = this.ult.getAnt();
                    this.ult.setProx(null);
                }
                else {
                    atual.getProx().setAnt(atual.getAnt());
                    atual.getAnt().setProx(atual.getProx());
                }
        this.quantNos--;
        return true;
    }
    public String toString(){
        String msg="";
        NoDupla atual = this.prim;
        while (atual != null){
            msg += atual.getInfo().getChave()+"\n";
            atual = atual.getProx();
        }
        return msg;
    }
}
    
asked by anonymous 18.05.2018 / 01:02

1 answer

0

It was probably done this way because the intention is that you can only create an empty list (without any node).

The list always starts empty, and as you add and remove elements, the number of nodes is updated. Note that only methods that insert or remove elements update the quantity value (either incrementing with ++ or decrementing with -- ). The only problem, in my opinion, is the setQuantNos method, but we'll leave it to the end.

Suppose you create a constructor that receives the number of nodes as a parameter:

public ListaDupla(int quantidade){
    this.prim = null;
    this.ult = null;
    this.quantNos = quantidade;
}

Then you create a list of 100 elements:

ListaDupla lista = new ListaDupla(100);

The list size is 100, but what are the 100 elements of it? I have not yet entered any. The size indicates 100, but actually does not have any ( prim and ult are null), which makes the list state inconsistent, as the value of quantNos indicates that there are more elements than facts exist. / p>

If I enter 1 element, the amount will be incremented and becomes 101. But I only inserted 1 element, so the list will continue to be inconsistent. How the list was implemented only works if it always starts with a zero amount.

I just think that the setQuantNos method does not make much sense there, since who controls the number of nodes are the methods that insert or remove an element.

If I can set the size of the list directly, without inserting or removing any element, it leaves the internal state of the list inconsistent. For example, if I create a list, I do not add any elements and I call setQuantNos(100) , it will find that it has 100 elements, even though I have not entered any (same problem of having a constructor that receives the quantity). >     

18.05.2018 / 02:54