#include <stdio.h>
#include <stack>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define OK 1
#define ERROR 0
typedef int SElemType;
struct SqStack
{
int *base;
int *top;
int stacksize;
};
int InitStrack(SqStack &S){
S.base = new SElemType[STACK_INIT_SIZE];
if(!S.base){
exit(1);
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
int Push(SqStack &S,SElemType e){
if(S.top - S.base == S.stacksize){
int *newbase;
newbase=new int[S.stacksize*2];
if(newbase==0)
return 0;
for (int i=0;i<=S.stacksize-1;i++)
{
newbase[i]=S.base[i];
}
delete S.base;
S.base=newbase;
S.top=&S.base[S.stacksize];
S.stacksize *=2;
}
*S.top=e;
S.top++;
return OK;
}
int Pop(SqStack &S){
if(S.top == S.base){
printf("空栈!");
return ERROR;
}
--S.top;
return OK;
}
SElemType GetTop(SqStack S){
if(S.top != S.base){
return *(S.top - 1);
}
return 00;
}
void Print(SqStack S)
{
int *p=S.base;
while (p<S.top)
{
printf("%d ",*p);
p++;
}
printf("
");
}
int main(void){
SqStack S;
int x,y;
InitStrack(S);
printf("Enter Numbers:
");
while(true){
scanf("%d",&x);
if(x == 9999){
break;
}
Push(S,x);
}
printf("The stack elems:");
Print(S);
printf("1.入栈:");
scanf("%d",&x);
Push(S,x);
printf("The stack elems:");
Print(S);
y = GetTop(S);
printf("2.取得栈顶元素:%d
",y);
printf("3.出栈:
");
Pop(S);
printf("The stack elems:");
Print(S);
}