Total number of elements in a stack

Asked

Viewed 776 times

2

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

I have this code here, but it’s 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??
}

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
}

Browser other questions tagged

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