Total number of elements in a stack

2

I want to know how to return the total number of elements in a stack on the screen. For example: I have a vector stack of size 10 and stacked 6 elements in it, it will have to return the total of 6 elements.

I have this code here, but it is not returning me correctly:

void pilha_imprime (tipo_pilha *pilha)
{
   int i;
   for (i=pilha->topo-1; i>=0; i--)
      printf(“%f\n”, pilha->pilha[i]);
   return i;      //tem retorno i??
}
    
asked by anonymous 30.03.2017 / 15:20

1 answer

3

Generic method for returning stack elements:

//Count the number of items in the stack
int countitem()
{
    ListElem *i;
    int t=0;
    i=pfirst; //point to the first item then move it to the next

    while(i!=NULL){
       t=t+1; //increment
       i=i->next;
     }

    return t; //return the number of item counted
}
    
30.03.2017 / 15:24