Function First element Stack

0

How do I make a function return the first element of a stack ?

For example:

I add integer values in my vector size 5 stack.

3
4
5
6
7

You have to return the first element inserted in the stack, in this case the 3rd.

I made this one:

float retornatopo ( struct Pilha *p ){

if(p->topo < 10)
return p->pElem [p->topo - 1];
else
    return -1;

}

But this return the top of the stack, I need the first element ( FIFO ).

    
asked by anonymous 29.03.2017 / 20:41

1 answer

1

You can simply do a function that returns the first element this way:

float primeiroElemento ( struct Pilha *p ) {
    if( p->topo > 0 )
        return p->Elem[0];

    return -1;
}

The above function checks for elements in the stack, if any, returns the first. Otherwise it returns -1;

You can do this quietly. But if your goal is to remove the first element and keep the rest of the elements in the same order , I disagree use the function I mentioned. In fact, it only returns the value that is at position 0 on the stack. If you want to remove the element this function is not good.

    
30.03.2017 / 04:36