Insert element Row C

0

How do I "Insert an element (returning 1 if the insertion has succeeded, and -1 otherwise) in a Row"

I did this, what is missing:

void enfilerar(tipo_fila *fila, int valor)
{
    if (fila->fim<TAM_FILA)
    {
        fila->fim++;
        fila->fila[fila->fim]=valor;
    }
    else
    {
        printf("Fila cheia!");
    }
}
    
asked by anonymous 27.03.2017 / 13:51

1 answer

1

You can do it in two ways:

1 -

int enfilerar(tipo_fila *fila, int valor)
{
    if (fila->fim<TAM_FILA)
    {
        fila->fim++;
        fila->fila[fila->fim]=valor;
        return 1;
    }
    else
    {
        printf("Fila cheia!");
        return 0;
    }
}

2 - (Recommended)

int enfilerar(tipo_fila *fila, int valor)
{
    int retorno = 0;

    if (fila->fim<TAM_FILA)
    {
        fila->fim++;
        fila->fila[fila->fim]=valor;
        retorno = 1;
    }
    else
    {
        printf("Fila cheia!");
    }

    return retorno;
}

Both ways will work, however it is recommended to return values in only 1 moment of the function. The second way follows this recommendation.

    
29.03.2017 / 02:12