|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 素食蛤蟆 于 2018-4-24 22:56 编辑
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCRESEMENT 10
typedef int SElemtype;
typedef struct
{
SElemtype *base;
SElemtype *top;
int Stacksize;
}SqStack;
int InitStack(SqStack S)
{
S.base=(SElemtype *)malloc(STACK_INIT_SIZE*sizeof(SElemtype));
if(!S.base)
{
exit(0);
}
S.top=S.base;
S.Stacksize=STACK_INIT_SIZE;
return 1;
}
int Push(SqStack S,SElemtype e)
{
if(S.top-S.base>=S.Stacksize)
{
S.base=(SElemtype *)realloc(S.base,(S.Stacksize+STACKINCRESEMENT)*sizeof(SElemtype));
S.top=S.base+S.Stacksize;
S.Stacksize+=STACKINCRESEMENT;
}
*S.top++=e;//执行到此处时卡死
return 1;
}
int Pop(SqStack S,SElemtype num)
{
if(S.top==S.base)
{
exit(0);
}
num=*--S.top;
return num;
}
int main()
{
int i;
int e;
//int temp;
int num;
SqStack(S);
InitStack(S);
for(i=0;i<5;i++)
{
printf("please input the elem you what to push into the stack:\n");
scanf("%d",&e);
Push(S,e);
}
for(i=0;i<5;i++)
{
Pop(S,num);
printf("the %d th number is %d \n",5-i,num);
}
system("PAUSE");
return 1;
} |
|