#include <stdio.h>
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERLOW -2
typedef int SDataType;
typedef int Status;
typedef struct
{
SDataType *base;
SDataType *top;
int StackSize;
}SqStack;
Status initstack(SqStack &s)
{
s.base=(SDataType *)malloc(STACK_INIT_SIZE*sizeof(SDataType));
if(!s.base) return 0;
s.top=s.base;
s.StackSize=STACK_INIT_SIZE;
return 1;
}
Status push(SqStack &s)
{
SDataType *p;
int i,k,n=1;
printf("输入栈元素的个数\n");
scanf("%d",&n);
printf("请输入%d个栈元素",n);
for(i=0;i<n;i++)
{
scanf("%d",&k);
if(s.top-s.base==s.StackSize)
{
p=(SDataType *)realloc(s.base,(s.StackSize+STACKINCREMENT)*sizeof(SDataType));
if(!p) return 0;
s.base=p;
s.top=s.base+s.StackSize;
s.StackSize+=STACKINCREMENT;
}
*(s.top)=k;
s.top++;
}
return 1;
}
Status Pop(SqStack &s)
{
int e;
while(s.top!=s.base)
{
s.top--;
e=*(s.top);
printf("%d ",e);
}
return 0;
}
void main()
{
SqStack s;
int e,i,k;
initstack(s);
push(s);
printf("Output the element:\n");
Pop(s);
printf("\n");
}
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERLOW -2
typedef int SDataType;
typedef int Status;
typedef struct
{
SDataType *base;
SDataType *top;
int StackSize;
}SqStack;
Status initstack(SqStack &s)
{
s.base=(SDataType *)malloc(STACK_INIT_SIZE*sizeof(SDataType));
if(!s.base) return 0;
s.top=s.base;
s.StackSize=STACK_INIT_SIZE;
return 1;
}
Status push(SqStack &s)
{
SDataType *p;
int i,k,n=1;
printf("输入栈元素的个数\n");
scanf("%d",&n);
printf("请输入%d个栈元素",n);
for(i=0;i<n;i++)
{
scanf("%d",&k);
if(s.top-s.base==s.StackSize)
{
p=(SDataType *)realloc(s.base,(s.StackSize+STACKINCREMENT)*sizeof(SDataType));
if(!p) return 0;
s.base=p;
s.top=s.base+s.StackSize;
s.StackSize+=STACKINCREMENT;
}
*(s.top)=k;
s.top++;
}
return 1;
}
Status Pop(SqStack &s)
{
int e;
while(s.top!=s.base)
{
s.top--;
e=*(s.top);
printf("%d ",e);
}
return 0;
}
void main()
{
SqStack s;
int e,i,k;
initstack(s);
push(s);
printf("Output the element:\n");
Pop(s);
printf("\n");
}