Data Structure, Circular Queue. Questions about the Queue and Queue methods

Asked

Viewed 447 times

1

I’m not getting the two blocks else in the methods Desenfileirar and Enfileirar. More specifically the lines:

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

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

Why use of the operator %? It would not be enough just to add +1 at the 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;
        }
    }

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 rest of the division, 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 for 3, the words:

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

Will add up to 1 (++) to this.frente, which is now worth 4, and then takes the rest of the division by this.info.length (that is, the rest of the division of 4 by 4, which is zero).

If I didn’t have the % and you only add 1, this.frente would have the value 4 (and then 5, and 6...)

Using %, you ensure 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 were 4, you would try to access an invalid array position (since its size is 4, so the valid positions are 0 to 3).


I don’t particularly like to wear ++ along with other expressions as it may get a little confusing to understand. I prefer to do it more explicitly, as for example:

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

    Attention that the +1 has to be inside the % to not get out of the valid sizes, so (this.frente + 1) % this.info.length;

  • @Isac Yes, lack of attention from me. I’ve corrected, thank you!

Browser other questions tagged

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