简体   繁体   中英

Pop function not working in stack

I am trying to execute the pop function but my pop function is not working.

I run pop and then show and it is printing only 0

typedef struct Stack
{
  int top;
  int elements[20];
}
stack;
stack s;
void pop()
{
  s.top--;
}


 void show() 
 { 
    while(s.top>=0)
    { 
      printf("%d\n",s.elements[s.top]); 
      s.top--; 
    }
 } 

The function

void pop()

has a void return type. So it is not returning an object for you to print.

pop function should be

int pop()
{
    if(s.top == -1)  // -1 is delimiter for stack to be empty 
    {
         printf("Stack is empty\n");
         return -1;
    }
    return s.elements[s.top--];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM