Data Structure, Circular Row. Doubts about the Debug and Queuing methods

1

I can not understand the two else blocks in methods Desenfileirar and Enfileirar . More specifically the lines:

this.tras = (++this.tras % this.info.length);

this.frente = (++this.frente % this.info.length);

Why use the % operator? Would not it be enough to add +1 to this.frente or this.tras ?

public class FilaCircular {

        private Item[] info;
        private int frente;
        private int tras;
        private int tamanho;

        public FilaCircular(int qte) {
            this.frente = 0;
            this.tras = 0;
            this.tamanho = 0;
            this.info = new Item[qte];
        }

        public Item getInfo() {
            return this.info[this.frente]; //sempre aponta para frente
        }

        public int getFrente() {
            return this.frente;
        }

        public int getTras() {
            return this.tras;
        }

        public int getTamanho() {
            return this.tamanho;
        }

        public boolean eVazia() {
            return this.tamanho == 0;
        }

        public boolean eCheia() {
            return (this.tamanho == this.info.length);
        }

        public boolean enfileirar(Item elem) {
            if(this.eCheia())
                return false;
            else {
                this.info[this.tras] = elem;
                this.tras = (++this.tras % this.info.length);
                this.tamanho++;
                return true;
            }
        }

        public Item desenfileirar() {
            Item no;
            if(this.eVazia()) 
                return null;
            else {
                no = this.info[this.frente];
                this.frente = (++this.frente % this.info.length);
                this.tamanho--;
                return no;
            }
        }

        public String toString() {
            String msg = "";
            int aux = this.frente;
            for(int i=1; i <= this.tamanho; i++) {
                msg += this.info[aux].getChave()+" ";
                aux = (++aux % this.info.length);
            }
            return msg;
        }
    }
    
asked by anonymous 16.05.2018 / 20:49

1 answer

3

If you just add 1, the value of the variable will grow indefinitely.

When using % , you are limiting the maximum size that the variable can have. This operator returns the remainder of the split, and is often used for such cases.

In this case, the code ensures that this.frente and this.tras are not larger than the size of the item array ( this.info ).

Example: If this.info has 4 elements, then this.info.length will be 4.

When this.frente is 3, the expression:

this.frente = (++this.frente % this.info.length);

It will add 1 (% with%) to% with%, which now becomes 4, and then takes the remainder of the division by ++ (that is, the remainder of the 4 by 4 division, which is zero ).

If you did not have this.frente and you only added 1, this.info.length would have the value 4 (and then 5, and 6 ...)

Using % , you guarantee that the value will always be between zero and 3. This is done because arrays are indexed to zero (the first position is zero, the second is 1, and so on), and if this.frente was 4, you would try to access an invalid position of the array (since its size is 4, then the valid positions are from 0 to 3).

I particularly do not like using % along with other expressions, as it may be confusing to understand. I'd rather do it more explicitly, for example:

this.frente = (this.frente + 1) % this.info.length;
    
16.05.2018 / 21:06