#include
#include
#define STACK_INT_SIZE 10
typedef int ElemType;
typedef struct
{ ElemType *base;
ElemType *top;
int stacksize; /*当前已分配的存储空间*/
} SqStack;
int InitStack(SqStack *S)//初始化栈
{ S->base=(ElemType *)malloc(STACK_INT_SIZE*sizeof(ElemType));
if(!S->base) return 0;
S->top=S->base;
S->stacksize=STACK_INT_SIZE;
return 1;
}
int Push(SqStack *S,ElemType e)//进栈
{ if(S->top-S->base==STACK_INT_SIZE)//栈满
return 0;
else
{ *(S->top)=e;
S->top++;
}
}
int Pop(SqStack *S,ElemType *e)//出栈
{ if(S->top==S->base)//栈空
return 0;
else
{ S->top--;
*e=*(S->top);
}
}
int CreateStack(SqStack *S)
{ int e;
if(S) //这里直接改为s
printf("Init Success!\n");
else
{ printf("Init Fail!\n");
return 0;
}
printf("input data:(Terminated by inputing a character)\n");
while(scanf("%d",&e)&&e!=0)//加个条件,不然会变为死循环
Push(S,e);
return 1;
}
void PrintStack(SqStack *S)//输出栈
{ ElemType e;
while(Pop(S,&e))
printf("%3d",e);
}
int main()
{ SqStack ss;
printf("1-createStack\n");
InitStack(&ss);//要先初始化栈再调用
CreateStack(&ss);
printf("2-Pop&Print\n");
PrintStack(&ss);
return 0;
}