Data Structure, Double Chained List. Doubt about the Double List Constructor

Asked

Viewed 174 times

1

Note: Code taken from a Data Structure booklet.

Why not pass the number of nodes from the Double Chained List by its Constructor (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;
    }
}

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 value of the quantity (incrementing with ++ or decreasing with --). The only problem, in my opinion, is the method setQuantNos, but let’s leave it to the end.

Suppose you create a constructor that receives the amount 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 haven’t entered any yet. The size indicates 100, but it doesn’t actually have any (prim and ult are null), which makes the status of the list inconsistent as the value of quantNos indicates that it has more elements than fact exist.

If I insert 1 element, the quantity will be incremented to 101. But I only entered 1 element, so the list will remain inconsistent. The way the list has been implemented only works if it always starts with zero amount.


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

If I can set the list size directly, without inserting or removing any elements, this leaves the internal state of the list inconsistent. For example, if I create a list, I don’t add any elements and call setQuantNos(100), it will find that it has 100 elements, even though I have not inserted any (the same problem of having a builder that gets the amount).

Browser other questions tagged

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