简体   繁体   English

弹出功能在堆栈中不起作用

[英]Pop function not working in stack

I am trying to execute the pop function but my pop function is not working. 我正在尝试执行pop函数,但是我的pop函数无法正常工作。

I run pop and then show and it is printing only 0 我运行pop然后显示,它仅打印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 pop功能应该是

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--];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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