dreamlike 发表于 2021-4-26 14:23:35

我想知道为什么我出栈的元素永远是1,实在没找到bug,学渣卑微求助大神

以下为源代码,我是按照书里把用到的栈函数一个个打出来的~
#include <stdio.h>
using namespace std;
#define maxsize 100
typedef int elemtype;
int x;
typedef struct
{
        elemtype elem;
        int top;
}Stack;
void InitStack(Stack *s)
{ s->top=0;
}
elemtype Push(Stack *s,elemtype x)
{
        if(s->top==maxsize)
        printf("Overflow");
        else
        s->elem=x;
}
elemtype Pop(Stack *s)
{if(s->top==0)
        return NULL;
        else
        {
                s->top--;
                return s->elem;
        }
}
elemtype StackEmpty(Stack *s)
{
        if(s->top>0)
        return 0;
        else
        return 1;
}
int main()
{
        Stack s;
        InitStack(&s);
        printf("请输入不为0的数,进栈\n");
        do {
        Push(&s,scanf("%d",&x));      
}

        while (x!=0);
    while(!StackEmpty(&s))
        printf("%d\t",Pop(&s));
}
求大神指正!{:5_100:}

ba21 发表于 2021-4-26 15:28:37

                        scanf("%d",&x);
      Push(&s, x);   

Baicai0626 发表于 2021-4-26 15:41:18

看不懂

nexcafe2 发表于 2021-5-3 22:24:37

scanf 的返回值并不是输入进去的值,scanf函数是c语言当中非常重要的格式化输入函数

其函数原型为:int scanf(const char *format, ...);

其函数返回值:成功格式化解析的个数

其调用格式为:scanf("<格式化字符串>", <参量表>);
页: [1]
查看完整版本: 我想知道为什么我出栈的元素永远是1,实在没找到bug,学渣卑微求助大神