#include
#include
#include
#define MAX 1024
typedef struct stack
{
int data[MAX];
int top;
}STACK;
void InitStack(STACK *s)
{
s->top = 0; //将顺序栈s置为空
}
int Size(STACK *s, int x)
{
s->top++;
if(x == 999)
return (s->top);
}
void StackEmpty(STACK *s) //判断栈是否为空
{
if(s->top > 0)
printf("栈空\n");
else
printf("栈不空\n");
}
void Push(STACK *s, int x)
{
//若栈s未满,则将元素x压入栈中;否则,栈的状态不变并给出错误信息
if(s->top == MAX - 1)
printf("Overflow");
else
s->data[s->top++] = x; //x进栈
}
int Pop(STACK *s)
{
//若栈s不空,则删去栈顶元素并返回元素值,否则返回0
if(s->top == 0)
return 0;
else
{
s->top--; //栈顶指针-1
return s->data[s->top--]; //返回原栈顶元素值
}
}
int Top(STACK *s)
{
if(s->top == 0)
return 0;
else
return (s->data[s->top - 1]);
}
void InitStack(STACK *s);
void Push(STACK *s, int x);
int Size(STACK *s, int x);
void StackEmpty(STACK *s);
int Pop(STACK *s);
int Top(STACK *s);
int main()
{
int x;
STACK *s;
s = (STACK *)malloc(sizeof(STACK));
InitStack(s);
printf("入栈:\n");
while(x!=999)
{
scanf("%d",&x);
Push(s,x);
Size(s,x);
}
StackEmpty(s);
printf("出栈:");
Pop(s);
printf("\n");
printf("读取栈顶元素:");
Top(s);
printf("\n");
return 0;
}
这个代码哪里错了?为什么入栈后,直接显示栈空
if(s->top > 0)
printf("栈空\n");
为啥大于0是栈空啊?咋想的?