Insert Row C element

Asked

Viewed 1,075 times

0

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

I did it, what’s missing:

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

1 answer

1

You can do it 2 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.

Browser other questions tagged

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