#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define error 0
#define ok 1
#define MAXSIZE 10
#define STACKINCREMENT 10
1.结构
typedef struct sqStack
{
int *base;
int *top;
int stackSize;
}sqStack;
2.初始化
int initStack(sqStack *S){
S->base=(int *)malloc(MAXSIZE*sizeof(int));
if(!S->base){
return error;
}
S->top=S->base;
S->stackSize=MAXSIZE;
return ok;
}
3.插入
void push(sqStack *s,int e){
if((s->top-s->base)>=s->stackSize){
s->base=(int *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(int));
s->top=s->base+s->stackSize;
s->stackSize=s->stackSize+STACKINCREMENT;
}
*(s->top)=e;
s->top++;
}
4.删除
int pop(sqStack *s,int *e){
if(s->top==s->base){
return error;
}
s->top--;
*e=*(s->top);
return ok;
}
5.清空
void clearStack(sqStack *s){
s->top=s->base;
}
6.销毁
void DestoryStack(sqStack *s){
while(s->base!=s->top){
free(s->base);
s->base++;
}
free(s->base);
s->base=s->top=NULL;
s->stackSize=0;
}
7.栈的长度
int stackLen(sqStack *s){
return s->top-s->base;
}
8.主函数
int main(){
sqStack s;
int e,sum=0;
initStack(&s);
for(int i=0;i<3;i++){
push(&s,1);
}
for(int i=0;i<3;i++){
pop(&s,&e);
sum+=e*pow(2,i);
}
printf("%d",sum);
return 0;
}