#include<malloc.h>
#include<stdio.h>
#define OVERFLOW -2
#define Stack_init_size 100
#define Stackincrement 10
typedef int SElemType;
typedef struct
{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
void main()
{
SqStackS;
voidInitStack(SqStack*S); //构造栈的声明
printf("构造一个栈并读入数据 \n");
InitStack(&S); //构造栈
printf("\n");
}
void InitStack(SqStack*S)//构造栈
{
inti,n;
SElemTypet;
S->base=(SElemType*)malloc(Stack_init_size*sizeof(SElemType));
S->top=S->base;
S->stacksize=Stack_init_size;
printf("请输入要读入栈内的数据个数:");
scanf("%d",&n);
intsum=0;
for(i=0;i<n;i++)
{
printf("输入元素:");
scanf("%d",&t);
*(S->top)=t;
printf("头指针元素为:%d\n",t);
S->top=S->top+1;
sum+=t;
}
printf("栈内元素和为:%d\n",sum);
}
|