C# - Collections - Enigma Exercise

Asked

Viewed 77 times

1

Hello, I was making a college exercise list and I came across this exercise, where you have a part of the code and it asks you to find out what code structure it belongs to. In my view, this seems to be a queue, but I can not fully understand and verify in the code, giving an example of an application. If anyone can help me, I appreciate your attention.

Part of the Code - Exercise:

public void remover() {
        if (!estaVazia()) {
            inicio++;
            qtdeElementos--;
       }
}

What structure is the code mentioned:

a) Stack

b) Row

c) Simple list

d) Double chained list

e) Vector

1 answer

3

Matheus, your understanding is correct, it’s an excerpt from a FILA implementation algorithm, as you may know, the concept tells us:

They are data structures of type FIFO (first-in first-out), where the the first item to be inserted shall be the first item to be removed, or is, add items at the end and remove from the beginning.

Basically the FILAS have two operations:

  1. Enqueue: insert an element at the end of the queue.
  2. Dequeue: remove an element from the queue start.

The snippet of your code is an operation Dequeue, removal, and as mentioned above, you will have to remove the first element for this inicio++ and after that you will have to decrease the queue size so qtdeElementos--

See its full code, in Java language, taken from this link:

import javax.swing.*;

class Fila{
    int inicio;
    int fim;
    int tamanho;
    int qtdeElementos;
    int f[];

    public Fila(){
        inicio = fim = -1;
        tamanho = 100;
        f = new int[tamanho];
        qtdeElementos = 0;
    }

    public boolean estaVazia(){
        if (qtdeElementos == 0){
            return true;
        }
        return false;
    }

    public boolean estaCheia(){
        if (qtdeElementos == tamanho - 1){
            return true;
        }
        return false;
    }

    public void adicionar(int e){
        if (! estaCheia()){
            if (inicio == -1){
                inicio = 0;
            }
            fim++;
            f[fim] = e;
            qtdeElementos++;
        }
    }

    public void remover(){
        if (! estaVazia() ){
            inicio++;
            qtdeElementos--;
        }
    }

    public void mostrar(){
        String elementos = "";
            for (int i = inicio; i<=fim; i++) {
                elementos += f[i]+ " - ";
            }
            JOptionPane.showMessageDialog(null, elementos);
    }
}

If there is any doubt, a picture is worth a thousand words, see the detailed explanations here:

inserir a descrição da imagem aqui

Browser other questions tagged

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